Calling native C++ function using DefinePInvokeMethod and returning a calculated value as a pointer/by reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marek

    #1

    Calling native C++ function using DefinePInvokeMethod and returning a calculated value as a pointer/by reference

    Hi
    I need to call a native function in a C++ dll from my C#. The C++ function
    declaration is shown below.

    The C# code that I have been trying to perform the call is also shown below.
    All it is trying to do is to pass in two integers, add them together and
    return the result in the third parameter (byref/as a pointer).

    The problem is how do I specify the third parameter to be a pointer to type
    int?

    Thanks for any help in advance.

    Marek

    C++ function declaration
    =============== =============== =============== =============== =============== =====
    extern "C"
    {
    __declspec( dllexport ) void WINAPI SumIntWithRef(i nt a, int b, int* pc)
    {
    *pc = a + b;
    }
    }

    C# call
    =============== =============== =============== =============== =============== =============== ==

    string dllName = @"NativeFunctio nDLL.dll";
    string entryPointName = "SumIntWithRef" ;

    //...Set up the return value:
    Type returnType = null;

    //...Set up the parameters:
    Type[] parameterTypes = new Type[3];
    object[] parameterValues = new object[3];

    parameterTypes[0] = typeof(int);
    parameterValues[0] = 2;

    parameterTypes[1] = typeof(int);
    parameterValues[1] = 3;

    parameterTypes[2] = typeof(int); //...How should this one be
    specified?
    parameterValues[2] = 0;

    // Create a dynamic assembly and a dynamic module
    AssemblyName assemblyName = new AssemblyName();
    assemblyName.Na me = "tempDll";
    AssemblyBuilder assemblyBuilder =
    AppDomain.Curre ntDomain.Define DynamicAssembly (assemblyName,
    AssemblyBuilder Access.Run);
    ModuleBuilder moduleBuilder =
    assemblyBuilder .DefineDynamicM odule("tempModu le");

    // Dynamically construct a global PInvoke signature
    // using the input information
    MethodBuilder methodBuilder =
    moduleBuilder.D efinePInvokeMet hod(entryPointN ame,
    dllName,
    MethodAttribute s.Static
    | MethodAttribute s.Public | MethodAttribute s.PinvokeImpl,
    CallingConventi ons.Standard,
    returnType,
    parameterTypes,
    CallingConventi on.Winapi,
    CharSet.Ansi);

    //...This global method is now complete:
    methodBuilder.S etImplementatio nFlags(MethodIm plAttributes.Pr eserveSig);
    moduleBuilder.C reateGlobalFunc tions();

    // Get a MethodInfo for the PInvoke method
    MethodInfo methodInfo = moduleBuilder.G etMethod(entryP ointName);

    // Invoke the static method and return whatever it returns
    ParameterInfo[] parameterInfo = methodInfo.GetP arameters();

    Object returnValue = new Object();

    try
    {
    methodInfo.Invo ke(null, parameterValues );
    MessageBox.Show (String.Format( "Value calculated: {0}",
    parameterValues[2]));


  • Willy Denoyette [MVP]

    #2
    Re: Calling native C++ function using DefinePInvokeMe thod and returning a calculated value as a pointer/by reference


    "Marek" <noreply@dnv.co m> wrote in message
    news:OMffeKXFGH A.1816@TK2MSFTN GP11.phx.gbl...
    | Hi
    | I need to call a native function in a C++ dll from my C#. The C++
    function
    | declaration is shown below.
    |
    | The C# code that I have been trying to perform the call is also shown
    below.
    | All it is trying to do is to pass in two integers, add them together and
    | return the result in the third parameter (byref/as a pointer).
    |
    | The problem is how do I specify the third parameter to be a pointer to
    type
    | int?
    |
    | Thanks for any help in advance.
    |
    | Marek
    |
    | C++ function declaration
    |
    =============== =============== =============== =============== =============== =====
    | extern "C"
    | {
    | __declspec( dllexport ) void WINAPI SumIntWithRef(i nt a, int b, int* pc)
    | {
    | *pc = a + b;
    | }
    | }
    |
    | C# call
    |
    =============== =============== =============== =============== =============== =============== ==
    |
    | string dllName = @"NativeFunctio nDLL.dll";
    | string entryPointName = "SumIntWithRef" ;
    |
    | //...Set up the return value:
    | Type returnType = null;
    |
    | //...Set up the parameters:
    | Type[] parameterTypes = new Type[3];
    | object[] parameterValues = new object[3];
    |
    | parameterTypes[0] = typeof(int);
    | parameterValues[0] = 2;
    |
    | parameterTypes[1] = typeof(int);
    | parameterValues[1] = 3;
    |
    | parameterTypes[2] = typeof(int); //...How should this one be
    | specified?
    | parameterValues[2] = 0;
    |
    | // Create a dynamic assembly and a dynamic module
    | AssemblyName assemblyName = new AssemblyName();
    | assemblyName.Na me = "tempDll";
    | AssemblyBuilder assemblyBuilder =
    | AppDomain.Curre ntDomain.Define DynamicAssembly (assemblyName,
    | AssemblyBuilder Access.Run);
    | ModuleBuilder moduleBuilder =
    | assemblyBuilder .DefineDynamicM odule("tempModu le");
    |
    | // Dynamically construct a global PInvoke signature
    | // using the input information
    | MethodBuilder methodBuilder =
    | moduleBuilder.D efinePInvokeMet hod(entryPointN ame,
    |
    dllName,
    |
    MethodAttribute s.Static
    || MethodAttribute s.Public | MethodAttribute s.PinvokeImpl,
    |
    CallingConventi ons.Standard,
    |
    returnType,
    |
    parameterTypes,
    |
    CallingConventi on.Winapi,
    |
    CharSet.Ansi);
    |
    | //...This global method is now complete:
    |
    methodBuilder.S etImplementatio nFlags(MethodIm plAttributes.Pr eserveSig);
    | moduleBuilder.C reateGlobalFunc tions();
    |
    | // Get a MethodInfo for the PInvoke method
    | MethodInfo methodInfo =
    moduleBuilder.G etMethod(entryP ointName);
    |
    | // Invoke the static method and return whatever it returns
    | ParameterInfo[] parameterInfo = methodInfo.GetP arameters();
    |
    | Object returnValue = new Object();
    |
    | try
    | {
    | methodInfo.Invo ke(null, parameterValues );
    | MessageBox.Show (String.Format( "Value calculated: {0}",
    | parameterValues[2]));
    |
    |



    Use the DefinePInvokeMe thod overload that takes a
    parameterTypeRe quiredCustomMod ifiers and set IsByRef for the third argument.

    Willy.


    Comment

    Working...