locked
Marshal RRS feed

  • Question

  •  Hi all

    I have a C header file with this code:

    typedef void ( CALLBACK* DOWNLOADING ) ( ULONG, LPARAM, LPBOOL);
    ....

    LPARAM cb_param,

    IStream** retval


    How can I use (Marshaling) it in C#.

    thanks

    Friday, December 12, 2008 2:01 AM

Answers

  • Hi,

    A callback function is a function defined by a DLL, bu implemented by the caller,that can be called by the DLL. The CALLBACK tag is defined within windef.h as follows:
    #define CALLBACK __stdcall
    The defination in you post define a function pointer,which can point to a funcion take 3 params.

    "typedef void ( CALLBACK* DOWNLOADING ) ( ULONG, LPARAM, LPBOOL);"

    You can search the .h file to see if there is one or more method take DOWNLOADING as parameter, or return a DOWNLOADING .

    For the parameters of the function, the ULONG type is a 32bits type defined in wtypes.h. I'm not sure if LPARAM is defined as long: #define PARAM long.LPBOOL is defined as long .So when you can use int32 to marshal the data.

    When you wish to interact with a tranditional Wind32 callback form managed code,you can follow the below steps:
    1. Define a delegate that represents the unmanaged callback.
    2. Build a Dllimport statement for the unmanaged export, using the delegate as the function pointer parameter.
    3. Assign a managed function to the delegate.
    4 Trigger the unmanaged export.


    For the  'CALLBACK* DOWNLOADING', we assume there is a method that takes a  DOWNLOADING pointer as parameter:
    native code:


    void _stdcall CallACallbackMethod(DOWNLOADING pf,ULONG a, LPARAM b, LPBOOL c)
    {
    (*pf)(a, b,  c);
    //Call the method pointed by pf
    }

    in  managed code:

    public delegate void CallbackMethodDelegate( Int32 , Int32, Int32);

    [DLLImport("......dll")]
    public static extern void CallACallbackMethod(CallbackMethodDelegate callback, Int32 a , Int32 b, Int32 c);

    CallbackMethodDelegate callback=new CallbackMethodDelegate(CallbackMethod);

    public static void CallbackMethod( Int32 a , Int32 b, Int32 c)
    {
    //....
    }

    For more information ,please see:http://msdn.microsoft.com/en-us/library/5zwkzwf4.aspx

    Best regards,
    Harry

    • Proposed as answer by Harry Zhu Tuesday, December 16, 2008 9:17 AM
    • Marked as answer by Harry Zhu Friday, December 19, 2008 6:29 AM
    Tuesday, December 16, 2008 9:17 AM
  • Hi,

    You can use the tool:
    http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx

    LPBOOL can be defined as 'ref int ' in c# , and LPARAM can be defined as 'int'.

    For IStream** retval,maybe you need use IntPtr, and you can check it with the tool.

    Best regards,
    Harry
    Monday, December 22, 2008 1:43 AM

All replies

  • [DllImport ( "kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto )]
    [return: MarshalAs ( UnmanagedType.Bool )] 

    // you don't need the last statement, it is just for illustration, because your return type is void. So, you don't Marshal anything. What you need to do it to match the input parameter types with your ULONG, LPARAM and LPBOOL. The type on the C# side is unimportant at all, what is important is the strict byte to byte correspondence of the types on both sides of the divide in length of the parametesr. I suspect your ULONG will be ulong, lpParam IntPtr (but I am not sure) and LPBOOL will be bool (on C# side).

    You will then call your functions like this:

    public static extern bool DeviceIoControl ( IntPtr hDevice,UInt32 dwIoControlCode,IntPtr lpInBuffer, Int32 nInBufferSize,IntPtr lpOutBuffer, Int32 nOutBufferSize,out uint lpBytesReturned, IntPtr lpOverlapped );


    AlexB
    • Proposed as answer by Harry Zhu Friday, December 19, 2008 6:05 AM
    • Marked as answer by Harry Zhu Friday, December 19, 2008 6:29 AM
    • Unmarked as answer by Khayralla AL-Athari Saturday, December 20, 2008 2:30 AM
    Friday, December 12, 2008 3:15 AM
  • AlexBB

    OK,

    How can I marshall type like

    LPARAM cb_param,

    IStream** retval

    thanks
    Friday, December 12, 2008 3:21 AM
  • typedef void ( CALLBACK* DOWNLOADING ) ( ULONG, LPARAM, LPBOOL);

     Can you explain it by using a code.
    Friday, December 12, 2008 3:30 AM
  • LPARAM is a message parameter. It is IntPtr. ULONG will become ulong, LPBOOL ==> bool.

    I guess it si private void downloading ( ulong ul, IntPtr whatever, bool bloolish ); Somethinbg line this. That was just a declaration. You call it like this:

    ulong ul = 25;
    IntPtr point = IntPtr.Zero; (or whatever). It could be a file handle
    bool boolish = true;

    Something like this. Just an example

    AlexB
    Friday, December 12, 2008 4:00 AM
  • Hi,

    A callback function is a function defined by a DLL, bu implemented by the caller,that can be called by the DLL. The CALLBACK tag is defined within windef.h as follows:
    #define CALLBACK __stdcall
    The defination in you post define a function pointer,which can point to a funcion take 3 params.

    "typedef void ( CALLBACK* DOWNLOADING ) ( ULONG, LPARAM, LPBOOL);"

    You can search the .h file to see if there is one or more method take DOWNLOADING as parameter, or return a DOWNLOADING .

    For the parameters of the function, the ULONG type is a 32bits type defined in wtypes.h. I'm not sure if LPARAM is defined as long: #define PARAM long.LPBOOL is defined as long .So when you can use int32 to marshal the data.

    When you wish to interact with a tranditional Wind32 callback form managed code,you can follow the below steps:
    1. Define a delegate that represents the unmanaged callback.
    2. Build a Dllimport statement for the unmanaged export, using the delegate as the function pointer parameter.
    3. Assign a managed function to the delegate.
    4 Trigger the unmanaged export.


    For the  'CALLBACK* DOWNLOADING', we assume there is a method that takes a  DOWNLOADING pointer as parameter:
    native code:


    void _stdcall CallACallbackMethod(DOWNLOADING pf,ULONG a, LPARAM b, LPBOOL c)
    {
    (*pf)(a, b,  c);
    //Call the method pointed by pf
    }

    in  managed code:

    public delegate void CallbackMethodDelegate( Int32 , Int32, Int32);

    [DLLImport("......dll")]
    public static extern void CallACallbackMethod(CallbackMethodDelegate callback, Int32 a , Int32 b, Int32 c);

    CallbackMethodDelegate callback=new CallbackMethodDelegate(CallbackMethod);

    public static void CallbackMethod( Int32 a , Int32 b, Int32 c)
    {
    //....
    }

    For more information ,please see:http://msdn.microsoft.com/en-us/library/5zwkzwf4.aspx

    Best regards,
    Harry

    • Proposed as answer by Harry Zhu Tuesday, December 16, 2008 9:17 AM
    • Marked as answer by Harry Zhu Friday, December 19, 2008 6:29 AM
    Tuesday, December 16, 2008 9:17 AM
  • Hi Harry Zhu

    thanks for your answer.

     

    Yes there is a method take DOWNLOADING as a parameter.
    for ULONG : 32bits type -> Int32
        LPARAM : ?
        LPBOOL: long (Are you sure)

    Just I need to instal the service to test it, any way this is what I am looking for

    last Question: How can I marshal or use

    IStream** retval


    thanks again.



    Saturday, December 20, 2008 2:29 AM
  • Hi,

    You can use the tool:
    http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx

    LPBOOL can be defined as 'ref int ' in c# , and LPARAM can be defined as 'int'.

    For IStream** retval,maybe you need use IntPtr, and you can check it with the tool.

    Best regards,
    Harry
    Monday, December 22, 2008 1:43 AM
  • thank Harry

    I will test it in 2009
    Thursday, December 25, 2008 5:39 PM