Cross-language C function call

xiaoxiao2021-03-06  14

These two days were confused by a DLL problem, just how to compile a DLL, so that it can be used in VC and VB. (The following description is based on Visual Studio 6.0 SP5, which exported in the DLL is a function of standard C implementation)

This is like this to write a function in the VC. This is true: if there is an extest () function to be exported, write extern "c" __declspec (dllexport) int extest ()

When using this DLL in the VC, if you need to call the EXTEST (), write extern "c" __declspec (dllimport) int extest () and you can use extest ().

This time, when I need to call this DLL EXTEST () in the VB engineering, I first declare: declare function excest lib "mydll.dll" () AS INTEGER then calls EXTEST () in the program, but after calling Always report a "run-time error '49", saying "Bad DLL CALLING CONVENTION".

After a pass in MSDN, the problem has been resolved, the method is as follows:

DLL Writing and Export Method * Write a DLL function in the VC to write: EXTERN "C" int _stdcall EXTEST () * then add: Extest in Exports of the project DEF file:

In the VC declaration, you can use extern "c" INT _STDCALL EXTEST ();

Declaration in VB is as follows, you can use the declare function excest lib "mydll.dll" () as integer

Explain two questions

I. "_ stdcall" function in C / C , the default CALLING Convention is: The parameter is pressed from right to left, and the stack is cleared by the caller. In Fortran, Pascal, Basic et al, the Calling Conventions of the function is: The parameter is pressed from right to left, and the stack is cleared by the called function.

Then use the _stdcall declaration function in C / C , you can specify the use of the C / C as the agreement of Fortran, etc., which does not appear abnormalities when calling these functions in other languages ​​such as Fortran, which is the key to the trouble. :)

Second, plus "EXTERN" C "" effect () "c" int _stdcall extest (); as the above prototype, the function name symbol will be processed by the VC compiler as _extest @ 0 int _stdcall EXTEST (); as the above protest, function The name symbol will be processed by the VC compiler as the compilation link environment of the EXTEST @@ ygxxz C language cannot handle the second way, so if your DLL is to apply in the C and C , you should use A naming method.

转载请注明原文地址:https://www.9cbs.com/read-48988.html

New Post(0)