c#调用c++;功能

c#调用c++;功能,c#,c++,C#,C++,我试着用 [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); 和Marshal.GetDelegateForFunctionPointer >从C++程序库中的C++库中获取C++函数指针。 一切似乎都正常,我可以在GetProcAddress和marshall.GetDelegateForFunctionPointer中获得

我试着用

[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
Marshal.GetDelegateForFunctionPointer

<> >从C++程序库中的C++库中获取C++函数指针。 一切似乎都正常,我可以在
GetProcAddress
marshall.GetDelegateForFunctionPointer
中获得非零的
IntPtr

但是,当我调用函数时,它将返回
IntPtr.Zero

当我在VisualStudio宿主中调试时,函数调用成功,没有任何问题。但是,当我在构建后运行程序而不使用Visual studio宿主时,函数返回
IntPtr.Zero

===========================================================================


在调试器外部运行时,hModule的值是多少

加载后的值库:252182528

GetProcAddress后的值:252326960


你确定你是在C++类中调用方法还是直接调用函数?如果是后者,只需使用[DllImport],而不用GetProcAddress

我调用C++中的一个类,调用C++中的一个方法来获得一个实例。 请提供实际的最小、完整和可验证的示例-很难解释为什么代码在没有看到它的情况下不能按您喜欢的方式运行

给你

public class NativeMethod
{
    private static ILog _logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    [DllImport("kernel32.dll")]
    public static extern void RtlMoveMemory(ref IntPtr dst, IntPtr src, int size);

    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);

    [DllImport("WS2_32.DLL")]
    public static extern int WSAStartup(int wVersionRequested, IntPtr lpWsaData);

    [DllImport("WS2_32.DLL")]
    public static extern int WSACleanup();

    public static void LoadFunction<T>(string dllPath, string functionName) 
    {
        var hModule = LoadLibrary(dllPath);
        var functionAddress = GetProcAddress(hModule, functionName);
        Delegate oFunc = Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
        IntPtr oFuncRtn = oFunc.DynamicInvoke();
        if(oFuncRtn == IntPtr.Zero)
            throw new Exception(" failed.");
    }        
}
公共类NativeMethod
{
私有静态ILog_logger=LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[DllImport(“kernel32.dll”)]
公共静态外部void rtlmovemory(参考IntPtr dst、IntPtr src、int size);
[DllImport(“kernel32.dll”)]
公共静态外部IntPtr加载库(字符串dlltoad);
[DllImport(“kernel32.dll”)]
公共静态外部IntPtr LoadLibraryEx(字符串lpFileName、IntPtr hFile、uint dwFlags);
[DllImport(“kernel32.dll”)]
公共静态外部IntPtr GetProcAddress(IntPtr hModule,string procedureName);
[DllImport(“kernel32.dll”)]
公共静态外部布尔自由库(IntPtr hModule);
[DllImport(“WS2_32.DLL”)]
公共静态外部程序int WSAStartup(int wVersionRequested,IntPtr lpWsaData);
[DllImport(“WS2_32.DLL”)]
公共静态外部输入WSACleanup();
公共静态void加载函数(字符串dllPath,字符串functionName)
{
var hModule=LoadLibrary(dllPath);
var functionAddress=GetProcAddress(hModule,functionalname);
函数指针的Delegate=Marshal.GetDelegateForFunctionPointer(函数地址,typeof(T));
IntPtr of unctn=oFunc.DynamicInvoke();
如果(OFUNCTN==IntPtr.Zero)
抛出新异常(“失败”);
}        
}

当你在调试器之外运行时,HMoad的值是什么?你确定你是在C++类中调用方法还是直接调用一个函数?如果是后者,只需使用
[DllImport]
而不用
GetProcAddress
就可以了,请提供实际的-很难解释为什么代码在没有看到它的情况下不能按您喜欢的方式运行。您不会说要传递给
LoadFunction
的函数名。需要检查的一件事是呼叫约定。另一种研究方法是,调用可能由于与函数指针->委托转换无关的原因而失败。但奇怪的是,我可以在Visual Studio托管下成功调用函数,当我运行编译程序时,它不能运行,当你在调试器外面运行时,HMULL的值是什么?你确定你是在C++类中调用方法还是直接调用一个函数?如果是后者,只需使用
[DllImport]
而不用
GetProcAddress
就可以了,请提供实际的-很难解释为什么代码在没有看到它的情况下不能按您喜欢的方式运行。您不会说要传递给
LoadFunction
的函数名。需要检查的一件事是呼叫约定。另一种研究方法是,调用可能由于与函数指针->委托转换无关的原因而失败。但奇怪的是,我可以在Visual Studio托管下成功调用函数,但在运行编译的程序时却不能