Interop problem (.NET 2.0 )

hello,

I have to load an unmanaged dll written in Delphi to my C# 2.0 application.

The unmanaged function has one parameter; pointer to an interface and return this pointer.

I am not sure how to declare the delegate and to use it.

My code below;

//here unsure about the signature

internal delegate MyDel (out IntPtr IModule);

IntPtr hModule = LoadLibrary("mydll.dll");

//if the handle is valid try to call members

if (hModule != IntPtr.Zero)

{

//get the function pointer

IntPtr fptr = GetProcAddress(hModule, "GetInterface");

if (fptr != IntPtr.Zero)

{

MyDel mgi = (MyDel)Marshal.GetDelegateForFunctionPointer(fptr, typeof(MyDel));

//here unsure

IntPtr ptrModuleInterface;

mgi(out ptrModuleInterface);

}

else

Marshal.GetHRForLastWin32Error();

FreeLibrary(hModule;

}

else

Marshal.GetHRForLastWin32Error();

Could you please help me to figure this out

Thanks,

Ada



Answer this question

Interop problem (.NET 2.0 )

  • Adah

    safecall is just a calling convention that means any [out,retval] parameters become function results. I cant see that being a problem. I am out of ideas.. can you provide a crash dump (use AdPlus, Debugging tools for windows, http://www.microsoft.com/whdc)



  • Solero

    Your delegate signature should match whatever the GetInterface signature is.

    public delegate void GetInterfaceDelegate(out IntPtr someptr);



  • Todd-TSIC

    thats because the myModuleDelg() delegate is returning an IntPtr. You need to marshal the IntPtr to the IInterface.

    If the Interface implements IUnknown you can use Marshal.GetObjectForIUnknown.

    eg IInterface _interface = (IInterface)Marshal.GetObjectForIUnknown(myModuleDelg());

    else use the Marshal.PtrToStructure method.

    eg. IInterface _interface = (IInterface)Marshal.PtrToStructure(myModuleDelg(),typeof(IInterface));



  • thrakazog

    Hi,

    Thanks Nikhil, now I understand better how the Marshalling of IntPtr works ,but I still get the same error.

    Maybe the problem resides on the Delphi dll side. The unmanged function is marked "safe call":

    function GetInterface: IInterface2; safe call;.

    IInterface2 is derived from IInterface.

    Any ideas

    Thanks,

    Ada


  • ammukarthik

    Hi,

    Thank you Nikhil, but my problem is a bit more complex, please see below:

    I have two cases:

    When the unmanaged function looks like:

    1. function GetInterface (out IInterface); works fine

    2. function GetInterface: IInterface; - got an error

    here is the code:

    //my delegate:

    public delegate IntPtr GetInterfaceDelegate();

    //get the handle to the module

    IntPtr handle = LoadLibrary("MyDLL.dll");

    //get the function pointer

    IntPtr fptr = GetProcAddress(hCardio, "GetInterface");

    //gets the delegate for fptr

    GetInterfaceDelegate myModuleDelg = (GetInterfaceDelegate)Marshal.GetDelegateForFunctionPointer(fptr, typeof(GetInterfaceDelegate));

    IInterface iInterface;

    iInterface = myModuleDelg();

    I have got this error;

    "Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

    Thanks,

    Ada


  • Interop problem (.NET 2.0 )