Help with strange PInvoke conversion

Hi, thanks in advance for your time and eventually for your help.

I have to wrap some c functions from a dll creating a C# assembly .
I tried many forums etc etc but i didn't find any help .

Here's the c funtion prototype :

short SessionNew( Session *pSession, SessionNewArgRec *pSessionNewArgRec );

where :

typedef struct _Session * Session ;
struct _Session is not defined anywhere, so i suppose that Session is a int ptr

SessionNewArgRec is a struct which im sure i do a good conversion of .


this is what i've written up to now :

[DllImport( "libxxx30.dll" , SetLastError = true, CallingConvention=CallingConvention.Cdecl)]
public static extern short SessionNew( IntPtr hSession, IntPtr lpSessionNewArg);

i don't think this is right, 'cause either it gives a memory violation exception, or i get an "invalid parameter" error code from the function .

here is the c sample code to help you understand what i have to do :

Session sess = NULL;
SessionNewArgRec args ;

// ... here i fill the SessionNewArgRec structure

SessionNew( &sess, &args );



PLEASE HELP ME IF YOU CAN ... im desperate ..



Answer this question

Help with strange PInvoke conversion

  • Dhaval-Patel

    Should i declare the IntPtr as IntPtr.Zero

    i mean something like :

    IntPtr hSessionPtr = IntPtr.Zero;

    api.SessionNew( out hSessionPtr, .... );


    sorry but im not a .net guru :P


  • NeroToxic

    Yes, that way you can tell that the DLL actually gave you a session handle back, it should be non-zero after the call.


  • RobertFogt

    Hmm, you're right, the argument is passed as struct _Session**. Try declaring the first argument as 'out IntPtr'.


  • xRuntime

    Sigh. The other interpretation is that it actually wants to write a chunk of memory to keep the session structure. You have no idea how much memory. Give it a lot:
    IntPtr hSession = Marshal.AllocHGlobal(2048);

    Any luck digging up that 'C' code yet that uses this DLL



  • aditi shah

    here's my code :

    IntPtr hSession = IntPtr.Zero;

    SessionNewArg args = new SessionNewArg();

    // ... i fill the args structure

    api.SessionNew( out hSession, args.getPtr() );

    // the args.getPtr() simply returns an IntPtr to the marshalld structure

    and here is the result :

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

    at xxx.api.SessionNew( IntPtr& hSession, IntPtr lpSessionNewArg)

    here's the message ...

    it seems that the function expects something inside hSession ...


  • ronnie2002c2c

    There's very little hope if you don't know what struct _Session looks like. Try to find some 'C' or C++ code that calls this DLL entrypoint...


  • Dylan Smith

    Sorry, maybe i didn't explain myself well ...

    struct _Session is not defined anywhere ... im sure of this ... so the code :

    typedef struct _Session * Session;

    means :

    typedef void * Session;

    i suppose that this is some kind of a strage way to type-define a general purpose handle ... due to the fact that i really don't know how to "translate" this from c to c#, i'm getting all sort of errors from the function ...

    the code :

    Session sess = NULL;
    SessionNewArgRec args ;

    // ... here i fill the SessionNewArgRec structure

    SessionNew( &sess, &args );


    is the only code you need to make this work in a c/c++ applications ...




  • Madhu Gandhi

    same exception as before ... even if i prealloc the chunk of memory ... i wrote that piece of C code, so there's nothing to dig about ... it works perfectly well in C ...


  • Help with strange PInvoke conversion