how to get address of a struct in DLL



  • Hi guys,

    i am writing a C# dll.
    In the DLL there a struct defined and will be used in a funtion PP_Ini().

    My question is how can i return the address of the struct used in the PP_Ini() to the Application which consums this DLL

    Or simply to say, if i defined a global struct in DLL, how the Application get the address of the struct in Dll.

    Thanks

    bag



  • hi~

    the C code looks like this

    dll code!

    // dll.c 
    struct blah; 
    .. 
    struct blah *dll_function (void) 
    { 
     ... 
     return &blah; 
    }
    

    application code!
    // main.c

    ... 
    void main (void) 
    { 
       struct blah *b = dll_function();  // get a pointer 
       b->... = ...;   // access the struct 
    }
    

    How such kinds of code looks like in C#.

    I need your help! Thank!

    bag



  • Hey, haven't got much time today^^ But a quick how-to:

    • Define a C# struct with an internal layout that matches the C struct 's. Do this by applying attributes such as System.Runtime.InteropServices.StructLayout to it. Specify the field alignment yourself if necessary.
    • Learn how to use unsafe code within C#. If you are familiar with C or even better with C++, this shouldn't take too long.
    • Import the function in your C# project using the DllImportAttribute class, specify an unsafe function signature and then use the returned value just as you would in C.

    I'm out. 😃


Log in to reply