Monday, March 22, 2010 10:10 AM
I'm coding a program which uses some dll files. When I try to run functions from the dll I get the next exception:
A call to PInvoke function '...SN_SetSource' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
I use the next code block to open the dll:
public static extern Int32 SN_SetSource(Int32 Source, Int32 board);
It is important to say that the same code works when I use .Net Framework 3.5.
I tried to replace the 'Int32' type with 'Int16' type, 'int' type, 'long' type, 'byte' type and more, but none worked!!
In additon, I also tried to cast the type I send to the SN_SetSource function to any of the above types, i.e:
and so on...
What is the problem and how can I fix it?
Monday, March 22, 2010 4:30 PMModerator
First find out how the SN_SetSource native signature (from C++ .h file) looks like (and post it here). Verify parameters, calling convention, etc.
Tuesday, March 23, 2010 6:25 AM
There is no .h file, Instead there is an SDK. In the SDK the SN_SetSource function is declared as follows:
"MID2250_API int SN_SetSource
(MID2250_SOURCE input, int board);
Selects between composite and S-video inputs.
enumerated input MID2250_SOURCE (see mid2250types.h). For model 2250 the allowed
values are MID2250_SOURCE_COMPOSITE_0 and MID2250_SOURCE_SVIDEO_0.
board number in the system (use 0 for single board setups).
0 on success, negative value if error (see mid2250types.h for error codes list). "
MID2250_SOURCE is defined like this:
enum MID2250_SOURCE : int
MID2250_SOURCE_COMPOSITE_0 = 0X0,
MID2250_SOURCE_SVIDEO_0 = 0X1,
MID2250_SOURCE_NUM = 0X2,
And last thig, the returned value (MID2250_API) is defined as follows:
#define MID2250_API extern "C" __declspec(dllexport)
#define MID2250_API extern "C" __declspec(dllimport)
#define MID2250_API __declspec(dllimport)
Tuesday, April 06, 2010 6:43 PMModerator
Apply all defines, etc. to get a full method declaration. Verify that it is correct - take it out into your own separate DLL, implement it (print all args and return a known value) and then use it the same way from managed code.
Another thing you can try to create a C++ DLL in the middle and call the function thrugh that. That way you can verify if it is your PInvoke issue, or SN_SetSource issue.
If there is no .h file, how can you call the function from C++? Maybe the SDK documentation is wrong and the actual signature is different.