在结构中编组 C++ win32 类型以在 C#

问题描述

我已经编写了一个可用的 C++ DLL。我想在 .NET C# 应用程序中使用它,但是我在编组类型方面遇到了困难,因为我对 C# 不太熟悉并且仍在学习。 DLL 在 C++ 编写的程序中使用时工作正常。

这是 DLL

#pragma once

#include <Windows.h>
#include <TlHelp32.h>

#ifdef MYDLL_EXPORTS
    #define MYDLL_API __declspec(dllexport)
#else
    #define MYDLL_API __declspec(dllimport)
#endif

struct clientInfo
{
    HANDLE processHandle = NULL;
    uintptr_t baseAddress = NULL;
};

extern "C" MYDLL_API bool hook(const wchar_t* window_name,const wchar_t* module_name,clientInfo &info);

基本上,该函数打开一个句柄,可以访问给定的窗口,并找到给定模块名称的模块基地址,并将它们放入作为函数参数中的引用传递的 clientInfo 结构中。成功返回真,不成功返回假。

至少我认为,我的问题不需要实现这个。 现在在 .NET Windows Forms C# 应用程序中,我设置了一个漂亮的表单,它会通知我按钮单击该函数是成功还是失败。使用我的 C# 代码,它可以编译,但效果不佳。 如何在 C# 中正确编组结构和函数

我不工作的 C# 代码

// Structure marshalling
 [StructLayout(LayoutKind.Sequential)]
 public struct clientInfo
 {
     public IntPtr processHandle;

     [MarshalAs(UnmanagedType.I4)]
     public uint gameBaseAddress;
 };

// Function marshalling
[DllImport("C:\\Users\\Cira\\source\\repos\\myDll\\Debug\\myDll.dll",CharSet = CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
private static extern bool hook(string window_name,string module_name,out clientInfo info);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)