Calling unmanged code in managed code

G

Guest

Hello

I'm currently working on a project, that contains one .net (VB) executable and a C++ (MFC) DLL. Now, what I want to do is to call the functions in the dll out of my managed code
//managed code (the unmanaged code declarations are just the same - but unmanaged)
Public Class Win32Call
<StructLayout(LayoutKind.Sequential)>
Class MYSTRUC
Public counter1 As Int6
Public counter2 As Int6
End Clas

Declare Function TestMarshalling Lib "test.dll" (ByVal stats As MYSTRUCT) As Boolea
End Clas

//[...
dim s as New Win32Calls.MYSTRUC
//filling s..
Win32Calls.TestMarshalling(s) '!!!

At this point, the debugger throws a System.NullReferenceException - can anyone tell me why?

Thanks a lo
James
 
M

Mattias Sjögren

James,
//managed code (the unmanaged code declarations are just the same - but unmanaged):

Please post the unmanaged declarations as well. Things aren't always
equal even if they look more or less the same in different languages.



Mattias
 
G

Guest

The unmanaged declarations are the following

typedef struct _MYSTRUCT
__int64 counter1
__int64 counter2
}MYSTRUCT

BOOL SetCurrentStatistics(MYSTRUCT stats

AfxMessageBox("Inside the DLL")
[...
 
G

Guest

Huhhh, sorry. I inserted the wrong code. But the original function TestMarshalling is just the same as SetCurrentStatistics
Any further ideas

James
 
M

Mattias Sjögren

James,
BOOL SetCurrentStatistics(MYSTRUCT stats)

If the function is using the default _cdecl calling convention, you
should add CallingConvention=CallingConvention.Cdecl to the C#
DllImport attribute.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top