471,605 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

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(int a, int b, int* pc)
{
*pc = a + b;
}
}

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

string dllName = @"NativeFunctionDLL.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.Name = "tempDll";
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("tempModule");

// Dynamically construct a global PInvoke signature
// using the input information
MethodBuilder methodBuilder =
moduleBuilder.DefinePInvokeMethod(entryPointName,
dllName,
MethodAttributes.Static
| MethodAttributes.Public | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
returnType,
parameterTypes,
CallingConvention.Winapi,
CharSet.Ansi);

//...This global method is now complete:
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
moduleBuilder.CreateGlobalFunctions();

// Get a MethodInfo for the PInvoke method
MethodInfo methodInfo = moduleBuilder.GetMethod(entryPointName);

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

Object returnValue = new Object();

try
{
methodInfo.Invoke(null, parameterValues);
MessageBox.Show(String.Format("Value calculated: {0}",
parameterValues[2]));
Jan 9 '06 #1
1 2197

"Marek" <no*****@dnv.com> wrote in message
news:OM**************@TK2MSFTNGP11.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(int a, int b, int* pc)
| {
| *pc = a + b;
| }
| }
|
| C# call
|
================================================== ==========================================
|
| string dllName = @"NativeFunctionDLL.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.Name = "tempDll";
| AssemblyBuilder assemblyBuilder =
| AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
| AssemblyBuilderAccess.Run);
| ModuleBuilder moduleBuilder =
| assemblyBuilder.DefineDynamicModule("tempModule");
|
| // Dynamically construct a global PInvoke signature
| // using the input information
| MethodBuilder methodBuilder =
| moduleBuilder.DefinePInvokeMethod(entryPointName,
|
dllName,
|
MethodAttributes.Static
|| MethodAttributes.Public | MethodAttributes.PinvokeImpl,
|
CallingConventions.Standard,
|
returnType,
|
parameterTypes,
|
CallingConvention.Winapi,
|
CharSet.Ansi);
|
| //...This global method is now complete:
|
methodBuilder.SetImplementationFlags(MethodImplAtt ributes.PreserveSig);
| moduleBuilder.CreateGlobalFunctions();
|
| // Get a MethodInfo for the PInvoke method
| MethodInfo methodInfo =
moduleBuilder.GetMethod(entryPointName);
|
| // Invoke the static method and return whatever it returns
| ParameterInfo[] parameterInfo = methodInfo.GetParameters();
|
| Object returnValue = new Object();
|
| try
| {
| methodInfo.Invoke(null, parameterValues);
| MessageBox.Show(String.Format("Value calculated: {0}",
| parameterValues[2]));
|
|

Use the DefinePInvokeMethod overload that takes a
parameterTypeRequiredCustomModifiers and set IsByRef for the third argument.

Willy.
Jan 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Michael | last post: by
2 posts views Thread by Martin Zenkel | last post: by
14 posts views Thread by Mike Labosh | last post: by
3 posts views Thread by Vinz | last post: by
27 posts views Thread by Terry | last post: by
5 posts views Thread by Fei Liu | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.