none
How to use 'LPVOID *' type varaible in c# while accessing a Function from C++ DLL in C#? please help! RRS feed

  • Question

  • Hi there,

               I am trying to call a Function from C++ dll  in  C# . i am facing so many problems with this.My application crashes while accessing 'LPVOID* ' type c++ variable from C# with 'ref IntPtr'.

    C++ Function has a variable of type 'LPVOID *'  and i need to pass a c# type variable.

    C++ Function:

    SccInitialize(
        LPVOID* ppContext,
        HWND hWnd,
        LPCSTR lpCallerName,
        LPSTR lpSccName,
        LPLONG lpSccCaps,
        LPSTR lpAuxPathLabel,
        LPLONG pnCheckoutCommentLen,
        LPLONG pnCommentLen
        )

    C# Method:

    SccInitialize(ref IntPtr ppContext,
                                     IntPtr hWnd,
                                     string lpCallerName,
                                     byte[] lpSccName,
                                      ref int lpSccCaps,
                                     byte[] lpAuxPathLabel,
                                     ref int CheckoutCommentLen,
                                     ref int CommentLen)

    now the problem comes with the "ppContext" varaible .  please help me with this an example on how to use 'LPVOID * ' type in c# and how to access it?

    Thanks in advance.

    • Edited by ManasMSDN Friday, December 10, 2010 4:01 PM title
    • Moved by Victor_Chen Monday, December 13, 2010 10:07 AM CLR issue (From:Visual Studio Source Control and SourceSafe)
    Friday, December 10, 2010 3:36 PM

Answers

  •  

    Hi ManasMSDN,

     

    Is there any exception thrown? Could you please tell us what the problem is?

     

    By the way, in C# part, you can allocate some memory using Marshal.AllocHGlobal method, for example:

     

    MyContext context = new MyContext();

    IntPtr ptr = Marshal.AllocHGlobal(context , ptr, true);

    SccInitialize(ref ptr);

     

    MyContext newContext = Marshal.PtrToStructure(ptr, typeof(MyContext));


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, December 20, 2010 5:06 AM
    Tuesday, December 14, 2010 8:15 AM

All replies

  • Hi ManasMSDN,

     

    Thanks for your post.

    I think this issue is more relevant to CLR, I'm going to move it.

    Hope you can get more useful information there.

    Best Regards,

    Ziwei Chen

     

     


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 13, 2010 10:06 AM
  •  

    Hi ManasMSDN,

     

    Is there any exception thrown? Could you please tell us what the problem is?

     

    By the way, in C# part, you can allocate some memory using Marshal.AllocHGlobal method, for example:

     

    MyContext context = new MyContext();

    IntPtr ptr = Marshal.AllocHGlobal(context , ptr, true);

    SccInitialize(ref ptr);

     

    MyContext newContext = Marshal.PtrToStructure(ptr, typeof(MyContext));


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, December 20, 2010 5:06 AM
    Tuesday, December 14, 2010 8:15 AM