May 05, 2024, 10:24:24 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


how to use Visual C++ Dlls

Started by yujinwunz, April 19, 2009, 11:35:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yujinwunz

me and my friend are working on a project, but we work on different languages. both of us have little experience, and i suggested that we use dlls as a way to do the project instead of leaning each other's language (frankly, obtaining each other's language) but somehow both of us can't use each other's dlls and .lib files.

I made a dll and a .lib for him, he also did for me. didn't work. we have linked them our own compilers. didn't work. i have read the guide about dlls in the user guide, done the "_", "CDECL", the whole lot. even to the .lib file that my friend game me. i CAN use my own EB dll, and he can use his, but we cant use each other's. he has searched his forums, but came up with nothing that helped.

my friend uses Visual C++, and i use EB
anyone done this successfully before?

yujin

Ionic Wind Support Team

Yes, but it does require a little thought and planning ahead of time.

First, import .lib files are not interchangeable between compilers, they all use their own format.  You have to create a .lib for Emergence from his C DLL using the "create import library" from the tools menu. 

Second, The C DLL should only export non decorated functions.  Tell him to use this define:

#define EXPORTC __declspec(dllexport)

And use the define for every function the DLL exports, like so:

bool EXPORTC somefunction()
{
}

In Emergence that would then be

declare cdecl import, _somefunction(), int

If he doesn't do it that way then the default Visual C naming convention uses decorations so the real function name depends on the number and size of the parameters. So if a function has two integer parameters then Visual C++ ends up calling it _somefunction@8 instead of just _somefunction. 

For his end he'll need to dynamically load your emergence DLL using LoadLibrary and indirectly call the exported functions by getting their address with GetProcAddress

As an example for your friend, this function is from the Emergence IDE, it loads menuedit.dll created in Emergence BASIC and calls an exported function indirectly.


void CEbasicApp::OnMenuEditor()
{
typedef BOOL(__cdecl *ShowMenuEditor)(HWND parent);
ShowMenuEditor pfnShowMenuEditor;
HINSTANCE hMenuEdit = GetModuleHandle( "menuedit.dll" );
if(hMenuEdit == NULL)
{
hMenuEdit = LoadLibrary("menuedit.dll");
}
if(hMenuEdit)
{
pfnShowMenuEditor = (int (__cdecl *)(struct HWND__ *))GetProcAddress( hMenuEdit, "ShowMenuEditor" );
if(pfnShowMenuEditor != NULL)
pfnShowMenuEditor(NULL);
}

}



Of course this may vary depending on what version of Visual C he is using.  Your functions in the Emergence DLL need to be created as cdecl (C declaration):

export myfunction
declare cdecl myfunction()

sub myfunction()
....
return
endsub

There is another method for him to use, it is possible to create a C++ import library from your DLL, not as easy as we have it in Emergence though.  Here are the Microsoft instructions:

http://support.microsoft.com/kb/131313

Specifically the section on creating a .DEF file, which then can be used to create an import .lib file for VC++ to use.  I always found it to be a bit of a pain, so I normally just load the DLL dynamically and indirectly call the functions.

Paul.

Ionic Wind Support Team

yujinwunz

thank you so much you are a legend! we were just about to give up! it is working perfectly now!
i understand that i am still using the buggy freeware, but i will buy it once i have the money.