Using C++ unmanaged dll in C#
I'm trying to access the functions in a C++ unmanaged dll and I'm struggling to get any results. Here is what I have far:
static unsafe void Main(string[] args)
{
IntPtr testIntPtr = aaeonAPIOpen(0);
var DevID = aaeonWdtGetDevID(testIntPtr);
Console.WriteLine("DevID: " + DevID.ToString()); //Does not work
byte pbData1 = 0;
byte pbData2 = 0;
Console.WriteLine("Before: {0} {1}", pbData1.ToString(), pbData2.ToString());
aaeon12VData(testIntPtr, &pbData1, &pbData2); //Notice long delay, but doesn't change pbData1 or pbData2
Console.WriteLine("After: {0} {1}", pbData1.ToString(), pbData2.ToString());
}
[DllImport("aonAPI.dll", EntryPoint = "?aaeonWdtGetDevID@@YAGPAX@Z", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern ushort aaeonWdtGetDevID(IntPtr hInst);
//C++ Code
//DLLAPI WORD aaeonWdtGetDevID(HANDLE hInst);
[DllImport("aonAPI.dll", EntryPoint = "?aaeon12VData@@YAJPAXPAE1@Z", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe IntPtr aaeon12VData(IntPtr hInst, byte* pbData, byte* pbData2);
// C++ code
//DLLAPI HRESULT aaeon12VData(HANDLE hInst, BYTE* pbData, BYTE* pbData2);
[DllImport("aonAPI.dll", EntryPoint = "?aaeonAPIOpen@@YAPAXK@Z", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr aaeonAPIOpen(uint reserved);
// C++ code
//DLLAPI HANDLE aaeonAPIOpen(DWORD reserved);
And here is how the C++ program uses the A开发者_StackOverflow社区PI for the 12V:
while(hInst == NULL)
{
hInst = aaeonAPIOpen(0);
}
unsigned char temp;
unsigned char temp2;
if(aaeon12VData(hInst,&temp,&temp2) == S_OK)
{
m_message.Format(_T("%d.%d"),temp2,temp);
}
Here is the dumpbin:
3 2 00001380 ?aaeon12VData@@YAJPAXPAE1@Z
5 4 00001020 ?aaeonAPIOpen@@YAPAXK@Z
10 9 00001040 ?aaeonWdtGetDevID@@YAGPAX@Z
I think if you make the following change to call "byref", you will solve your problem:
public static extern unsafe IntPtr aaeon12VData(IntPtr hInst, ref byte pbData, byte pbData2);
and:
aaeon12VData(testIntPtr, ref pbData1, ref pbData2);
Your C# code appears to reference mangled-names for entry points. That strikes me as very suspicious - likely wrong. Try it without the mangled names (use the pre-mangled names).
精彩评论