I have a c dll that i am trying to call from c# using Dllimport.
I continue to get "Attempted to read or write to protected memory" when the function is called. I have tried just about anything i can think of, so any help would be appreciated.
Here is the C code
struct DocProcHandle {
unsigned long Size;
void* UserHandle;
const char* DeviceName;
unsigned long DeviceTypeId;
unsigned long DeviceState;
unsigned long OpenFlags;
void* Reserved1;
};
typedef struct DocProcHandle *DPHandle;
BPS_DP_API DPHandle CDECLCALL_CONV DPOpenHandleEx(unsigned long deviceTypeId, const char* trkBaseWrite, const char* trkBaseConfig, const unsigned long openFlags);
Here is my c# code
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct DocProcHandle
{
ulong Size;
IntPtr UserHandle;
[MarshalAs(UnmanagedType.LPStr)] string DeviceName;
ulong DeviceTypeId;
ulong DeviceState;
ulong OpenFlags;
IntPtr Reserved1;
};
[DllImport("DocProc.dll", EntryPoint = "DPOpenHandleEx", ExactSpelling = false, CallingConvention =CallingConvention.Cdecl)]
private static extern IntPtr DPOpenHandleEx(ulong deviceTypeId, [MarshalAs(UnmanagedType.LPStr)] string trkBaseWrite, [MarshalAs(UnmanagedType.LPStr)] string trkBaseConfig, [In] ulong openFlags);
static public void OpenHandle()
{
string s = "C:\\temp";
IntPtr a = DPOpenHandleEx((ulong)7, s, s, 0);
}
It blows up calling the DPOpenHandleEx method.
Any ideas are appreciated.
What I have tried:
I added the additional items to the dllimport line to further refine it.
Changing the return type marshal it.