Hello there. I'm a bit new to creating DLL's. I've been learning a bit and I have created my own (finally). Well, my goal is to load my DLL to an application in VC++ and VB. I have successfully loaded my DLL in VB but having some trouble in VC++. The DLL that I have created is a simple one, the one with no ".h". From the tutorial that I have read, the function inside the DLL has a _stdcall on it.

int _stdcall sampleFunc(int iVar, char cVar){
    //Code here
    ...
}

The VB application has no trouble executing but the VC++ has trouble, a debug error pops up telling me that "The value of ESP was not properly saved across a functional call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention". Here's what confuses me, when I remove the _stdcall on the function, my VC++ app works fine and the my VB app crashes telling me "Bad DLL". Yep, ver baaad. *sigh*.

What I did to my VC++ application is explicitly linked my DLL to it. This is what I did.

typedef int (*myFunc)(int, char);
myFunc _myFunc;
HINSTANCE hMyLib = LoadLibrary("MyDLL.dll");

void anotherFunc(){
       int iVar = 0;
       char cVar;
        if(hMyLib){
		_myFunc = (myFunc)GetProcAddress(hMyLib , "sampleFunc");
		
		if(_myFunc){
			_myFunc(iVar, cVar);
		}

		FreeLibrary(hActLib);
	}else{
		printf("Library Failed!");
	}
}

Has anyone got any good advice for me? Kinda confused. Thanks! :)

int _stdcall sampleFunc(int iVar, char cVar){
    //Code here
    ...
}
typedef int (*myFunc)(int, char);

Your myFunc typedef does not match the DLL function's signature because you've omitted the calling convention from the typedef (by default, VC++ uses the _cdecl calling convention). So you need to have

typedef int (_stdcall *myFunc)(int, char);
commented: Explanation is good. The answer is flawless. +1

It works perfectly fine now! Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.