Reputation: 27
Win32 program as below:
#ifdef A_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
extern "C" DLL_API void EC_GetGin(int* icard);
void EC_GetGin(int* icard)
{
icard = 1;
}
C# code as below:
[DllImport("test.dll", EntryPoint = "EC_GetGin", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void EC_GetGin(ref int icard);
int icard = 0;
EC_GetGin(ref icard);
Why can't C# get 1 ?
Upvotes: 0
Views: 68
Reputation: 1
You need to modify the value, use *
with the pointer to get the value.
void EC_GetGin(int* icard)
{
*icard = 1;
}
Upvotes: 1
Reputation: 613442
Your C code modifies the pointer rather than writing to the variable it points to. Change it to
void EC_GetGin(int* icard)
{
*icard = 1;
}
The C# code would be cleaner if you declared the argument as out
rather the ref
.
Upvotes: 4
Reputation: 43916
In your C code you don't set the value in the memory location, but change the pointer in icard
(you let icard
point to memory location 1
).
To change the value in the memory location icard
points to use
*icard = 1;
Upvotes: 1