none
I get a TargetInvocationException when invoke a function by DynamicInvoke on Windows 7. RRS feed

  • Question

  • Environment: 64-bit Windows 7 + Visual Studio 10

    My application is developed by C#, and need to invoke some functions in an DLL developed by C++. 

    1.The original function is defined in DLL:

     

    char* WINAPI fnReturnPSTRParaA(char* value)

    {return value; }

    2.Import this function in C# application;

    [System.Runtime.InteropServices.DllImport("TestExternalDll.dll" ,EntryPoint="fnReturnPSTRParaA", CharSet=System.Runtime.InteropServices.CharSet.Ansi)]

    public static extern string __PBExternal_fnreturnpstrparaa(string value);

    3. Then invoke this function by a delegate function, which is called through DynamicInvoke.

    Every time I transfered "ANSI string" as parameter to this function, the exception will be raised.

    This application runs fine on Windows XP. Can anyone give me some advice? Thanks in advance.

    Best Regards,

    Bqli

     

     

    Friday, June 17, 2011 10:42 AM

Answers

  • You can try this:

        class Program

        {

            [DllImport("cpplib.dll", CharSet = CharSet.Ansi)]

            static extern IntPtr test(IntPtr str);

     

            static void Main(string[] args)

            {

                string str = "ANSI string";

     

                IntPtr buff = IntPtr.Zero;

                try

                {

                    buff = Marshal.StringToCoTaskMemAnsi(str);

     

                    IntPtr result = test(buff);

     

                    string ret_str = Marshal.PtrToStringAnsi(result);

     

                    Console.WriteLine(ret_str);

                }

                finally

                {

                    if (buff != IntPtr.Zero)

                    {

                        Marshal.FreeCoTaskMem(buff);

                    }

                }

               

                Console.ReadLine();

            }

     


    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 bqli Wednesday, June 29, 2011 9:03 AM
    Friday, June 24, 2011 3:37 AM

All replies

  • The managed signature works well on my side with a normal p/invoke, I'm using Win7 64bit + VS2010 + .NET 4.0 Client Profiler, the C++ dll is built as Win32, and the C# application is built as x86. How you invoke the C++ function in C# using DynamicInvoke?
    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.

    Monday, June 20, 2011 3:46 AM
  • Hi Eric,

    It looks like this exception is caused by a limitation of P/Invoke. The function like "char* func(char* value){return value; }" is unsupported. I found the related document in http://msdn.microsoft.com/en-us/library/ms235282.aspx

    Thanks for your response.

    Wednesday, June 22, 2011 9:20 AM
  • You can try this:

        class Program

        {

            [DllImport("cpplib.dll", CharSet = CharSet.Ansi)]

            static extern IntPtr test(IntPtr str);

     

            static void Main(string[] args)

            {

                string str = "ANSI string";

     

                IntPtr buff = IntPtr.Zero;

                try

                {

                    buff = Marshal.StringToCoTaskMemAnsi(str);

     

                    IntPtr result = test(buff);

     

                    string ret_str = Marshal.PtrToStringAnsi(result);

     

                    Console.WriteLine(ret_str);

                }

                finally

                {

                    if (buff != IntPtr.Zero)

                    {

                        Marshal.FreeCoTaskMem(buff);

                    }

                }

               

                Console.ReadLine();

            }

     


    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 bqli Wednesday, June 29, 2011 9:03 AM
    Friday, June 24, 2011 3:37 AM
  • We need provide a common solution to our users about DLL invocation, not a workaround for this scenario.

    Anyway, thanks a lot.

    Monday, June 27, 2011 5:15 AM