C#程序需要调用dll,但看起来参数out与值in相同。因此,我制作了此简短示例进行测试。 这是C ++ Dll代码: .h
#define EtrpDll extern "C" __declspec(dllexport)
EtrpDll void __fastcall WhyWrong(int &m);
.cpp
#include"Header.h"
#include<iostream>
void __fastcall WhyWrong(int &m) {
m++;
}
int main() {
int m = 0;
WhyWrong(m);
std::cout << m;
int y;
std::cin >> y;
}
和C#代码: .cs
using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
namespace DllTest
{
class Class1 {
[DllImport("Kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("Kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("Kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("Kernel32.dll")]
private static extern int GetLastError();
private static IntPtr hModule = IntPtr.Zero;
private static AssemblyName GbsAssemblyName;
private static AssemblyBuilder GbsAssemblyBuilder;
private static ModuleBuilder GbsModuleBuilder;
public enum ModePass {
ByValue = 0x0001,
ByRef = 0x0002
}
private IntPtr GFuncPtr = IntPtr.Zero;
private Type[] GParaType;
private ILGenerator GIL;
private ModePass[] GMdPass;
public object[] GObject;
private string Mname;
private static Type GReturn = typeof(void);
static void Main() {
Class1.DllIni();
Class1 G_WhyW = new Class1(new Type[1] { typeof(int) }, new Class1.ModePass[1] { Class1.ModePass.ByRef }, new object[1] { (int)0 });
G_WhyW.LoadFunc("WhyWrong");
Class1.GCreateGlbFunc();
G_WhyW.GObject[0] = (int)1;//Input
G_WhyW.InvokeDllFunc();
Console.WriteLine((int)G_WhyW.GObject[0]);
Console.ReadLine();
Class1.UnLoadDll();
}
public static void DllIni(){
hModule = LoadLibrary("DllTest.dll");
if (hModule == IntPtr.Zero) {
Console.WriteLine("DLL Not Loaded");
int e = GetLastError();
Console.WriteLine("Error Code: " + e);
}
GbsAssemblyName = new AssemblyName();
GbsAssemblyName.Name = "GbsCore";
GbsAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(GbsAssemblyName, AssemblyBuilderAccess.Run);
GbsModuleBuilder = GbsAssemblyBuilder.DefineDynamicModule("FuncGrp");
}
public static void GCreateGlbFunc() {
GbsModuleBuilder.CreateGlobalFunctions();
}
public void LoadFunc(string lpProcName) {
GFuncPtr = GetProcAddress(hModule, lpProcName);
if (GFuncPtr == IntPtr.Zero) {
Console.WriteLine("Function " + lpProcName + " Not Loaded");
}
Mname = lpProcName + "_T";
MethodBuilder GbsMethodBuilder = GbsModuleBuilder.DefineGlobalMethod(Mname, MethodAttributes.Public | MethodAttributes.Static, GReturn, GParaType);
GIL = GbsMethodBuilder.GetILGenerator();
if (GObject != null) {
for (int i = 0; i < GObject.Length; i++) {
switch (GMdPass[i]) {
case ModePass.ByValue:
GIL.Emit(OpCodes.Ldarg, i);
break;
case ModePass.ByRef:
GIL.Emit(OpCodes.Ldarga, i);
break;
default:
Console.WriteLine("Pass Mode Error");
break;
}
}
}
if (IntPtr.Size == 4) { GIL.Emit(OpCodes.Ldc_I4, GFuncPtr.ToInt32()); } //Platform
else if (IntPtr.Size == 8) { GIL.Emit(OpCodes.Ldc_I8, GFuncPtr.ToInt64()); }
else { throw new PlatformNotSupportedException(); }
GIL.EmitCalli(OpCodes.Calli, CallingConvention.FastCall, GReturn, GParaType);
GIL.Emit(OpCodes.Ret);
}
public void InvokeDllFunc() {
MethodInfo GbsMethodInfo;
if (GParaType == null) {
GbsMethodInfo = GbsModuleBuilder.GetMethod(Mname);
}
else {
GbsMethodInfo = GbsModuleBuilder.GetMethod(Mname, GParaType);
}
GbsMethodInfo.Invoke(null, GObject);//return void
}
public static void UnLoadDll() {
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
public Class1(Type[] T, ModePass[] MP, object[] OB) {
GParaType = T;
GMdPass = MP;
GObject = OB;
}
}
}
在调用功能WhyWrong之前,参数的值为1,但是我尝试的结果也为1。应该是2,不是吗?
答案 0 :(得分:0)
此动态方法的签名是void(int)
,而不是您想要的void(ref int)
,因为它是用new Type[]{typeof(int)}
初始化的。
支持使用反射来调用带有参考参数的方法(并修改了参数数组),因此完全摆脱了 ModePass 枚举,并在定义方法时使用typeof(int).MakeByRefType()
。 >
整个switch (GMdPass[i])
是不必要的。由于参数是通过引用提供的,因此将其简单地作为引用本身提供给调用的函数(ldarg
就足够了。)
顺便说一句,为什么不简单地使用Marshal.GetDelegateForFunctionPointer?它几乎和动态方法一样,但是应该更快。您只需要定义一个委托类型(非泛型)即可。
还不支持 FastCall 吗?