call dll function from another dll

Jul 23, 2012 at 3:45pm
Hi, im having problem to call a dll function from dll

the code look like this..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//file : a.cpp

int main(){

HINSTANCE dllHandle = LoadLibrary(TEXT("b.dll"));
typedef void (__stdcall * test)(void);
FARPROC pTest = GetProcAddress(HMODULE (dllHandle),"test");

test obj;
obj = test(pTest);
obj();

FreeLibrary(dllHandle);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//file : b.dll

extern "C" __declspec( dllexport ) void test(){

HINSTANCE dllHandle = LoadLibrary(TEXT("c.dll"));
typedef void (__stdcall * testAgain)(void);
FARPROC pTestAgain = GetProcAddress(HMODULE (dllHandle),"testAgain");

testAgain obj;
obj = testAgain(pTestAgain);
obj();

printf("success call c.dll");
FreeLibrary(dllHandle);

}

1
2
3
4
5
//file : c.dll

extern "C" __declspec( dllexport ) void testAgain(){
  printf("test again & again");
}

When the program executed, i can see the text : "test again & again"
but after that the program stopped working,
i dont have any clue what happen here..

is there something wrong with the code ?
Any suggestions would be much appreciated, Thanks
Jul 23, 2012 at 4:17pm
I see pTestAgain uses the __stdcall convention, but I don't see that calling convention in c.dll. The same thing goes for the test typedef: The typedef uses it but the function doesn't. In C and C++, the default calling convention is cdecl, so this is probably the cause of failure.
Jul 23, 2012 at 4:25pm
Are either of those functions the entry point for their respective DLL's by chance? Because that would cause a problem to.
Jul 24, 2012 at 9:02am
@webJose

Tq for pointing __stdcall.. :)
the program run well without __stdcall

typedef void (__stdcall * testAgain)(void);

This case is close & solved,
Thank you so much
Last edited on Jul 24, 2012 at 9:03am
Topic archived. No new replies allowed.