从 c# 代码传递结构引用以调用 c++在其原型中接受结构引用的 DLL 函数

发布于 2024-10-25 19:11:27 字数 541 浏览 1 评论 0原文

我在 c++ DLL 中有一个函数,它具有以下原型

int function(RefPar &params);

我如何使用“DLLImport”从 ac# 程序调用此函数。

当我尝试如下所示时,在 Visual Studio 2008 中运行时发生 AccessViolationException ..

[DllImport("VistaGMMDLL.dll", EntryPoint = "function"]
unsafe static extern int function(ref RefPar params);

并调用为..

int ret=function(ref params);

注意:RefPar结构有很多 无符号整数值和 1 个枚举 作为其成员的价值。

请任何人帮助我正确调用该函数..

I hav a function in c++ DLL that has following prototype

int function(RefPar ¶ms);

how can i call this function from a c# program using "DLLImport".

when i tried like below, AccessViolationException happened while running in visual studio 2008..

[DllImport("VistaGMMDLL.dll", EntryPoint = "function"]
unsafe static extern int function(ref RefPar params);

and called as..

int ret=function(ref params);

Note:RefPar structure has many
unsigned integer values and 1 enum
value as its members.

pls anyone help me to call the function correctly..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-11-01 19:11:27

有几件事让我突然想到。首先,我不明白为什么你需要使用不安全。其次,您可能存在调用约定不匹配的情况,即 C++ 中的 cdecl 和 C# 中的 stdcall。

我会这样做:

C++

struct RESOURCE_PARAMETERS{
    unsigned int uSurfaceHeight;
    unsigned int uSurfaceDepth;
    unsigned int uSurfaceWidth;
    unsigned int uMSAAHeight;
    unsigned int uMSAAWidth;
    unsigned int uArraySize;
    unsigned int uNumSamples;
    unsigned int uMaxLod;
    unsigned int uBpp;
    unsigned int uprefFlag;
    unsigned int uusageFlag;
    RESOURCE_TYPE_REC ResourceType;
    int ResourceFormat;
    int iBuildNumber;
};

int function(RefPar ¶meters)
{
}

C#

[StructLayout(LayoutKind.Sequential)]
public struct RESOURCE_PARAMETERS
{
    uint uSurfaceHeight;
    uint uSurfaceDepth;
    uint uSurfaceWidth;
    uint uMSAAHeight;
    uint uMSAAWidth;
    uint uArraySize;
    uint uNumSamples;
    uint uMaxLod;
    uint uBpp;
    uint uprefFlag;
    uint uusageFlag;
    [MarshalAs(UnmanagedType.U4)]
    ResourceType ResourceType;
    int ResourceFormat;
    int iBuildNumber;
} 

[DllImport("VistaGMMDLL.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function(ref RESOURCE_PARAMETERS parameters);

RESOURCE_PARAMETERS parameters = new RESOURCE_PARAMETERS();
int result = function(ref parameters);

我不确定枚举在 C++ 大小上有多大。这就是我在 C# 代码中放置显式 MarshalAs 的原因。如果它只是一个字节,则使用 UnmanagementType.U1 代替。我相信你明白了。

如果您的 C++ 函数将其参数视为输入/输出参数,则在 C# 端使用 ref 是正确的。如果它实际上是一个输出参数,那么将代码更改为如下:

[DllImport("VistaGMMDLL.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function(out RESOURCE_PARAMETERS parameters);

RESOURCE_PARAMETERS parameters;
int result = function(out parameters);

A couple of things jump out at me. First of all I don't see why you need to use unsafe. Secondly, you probably have a calling convention mismatch, cdecl in the C++ and stdcall in the C#.

I'd do it like this:

C++

struct RESOURCE_PARAMETERS{
    unsigned int uSurfaceHeight;
    unsigned int uSurfaceDepth;
    unsigned int uSurfaceWidth;
    unsigned int uMSAAHeight;
    unsigned int uMSAAWidth;
    unsigned int uArraySize;
    unsigned int uNumSamples;
    unsigned int uMaxLod;
    unsigned int uBpp;
    unsigned int uprefFlag;
    unsigned int uusageFlag;
    RESOURCE_TYPE_REC ResourceType;
    int ResourceFormat;
    int iBuildNumber;
};

int function(RefPar ¶meters)
{
}

C#

[StructLayout(LayoutKind.Sequential)]
public struct RESOURCE_PARAMETERS
{
    uint uSurfaceHeight;
    uint uSurfaceDepth;
    uint uSurfaceWidth;
    uint uMSAAHeight;
    uint uMSAAWidth;
    uint uArraySize;
    uint uNumSamples;
    uint uMaxLod;
    uint uBpp;
    uint uprefFlag;
    uint uusageFlag;
    [MarshalAs(UnmanagedType.U4)]
    ResourceType ResourceType;
    int ResourceFormat;
    int iBuildNumber;
} 

[DllImport("VistaGMMDLL.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function(ref RESOURCE_PARAMETERS parameters);

RESOURCE_PARAMETERS parameters = new RESOURCE_PARAMETERS();
int result = function(ref parameters);

I'm not sure how big the enum is on the C++ size. That's why I've put an explicit MarshalAs in the C# code. If it's just a single byte, then use UnmanagedType.U1 instead. I trust you get the idea.

If your C++ function treats its parameter as an in/out parameter then using ref on the C# side is correct. If its actually an out parameter then change the code to be like this:

[DllImport("VistaGMMDLL.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int function(out RESOURCE_PARAMETERS parameters);

RESOURCE_PARAMETERS parameters;
int result = function(out parameters);
心的憧憬 2024-11-01 19:11:27

尝试这样:

[StructLayout(LayoutKind.Sequential)]
public struct RefPar
{
    UInt32 uint1;
    UInt32 unti2;
    ....
} 


[DllImport("VistaGMMDLL.dll", EntryPoint = "function"]
unsafe static extern int function(IntPtr params);

//calling

//fill the refParStructure
//create the IntPtr  

refParStruct rs = new RefPar();

IntPtr refparPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(refPar)));


Marshal.StructureToPtr(refParStruct, refparPtr,false);
ret=function(refParPtr);

如果您需要更多详细信息,请告诉我

Try in this way:

[StructLayout(LayoutKind.Sequential)]
public struct RefPar
{
    UInt32 uint1;
    UInt32 unti2;
    ....
} 


[DllImport("VistaGMMDLL.dll", EntryPoint = "function"]
unsafe static extern int function(IntPtr params);

//calling

//fill the refParStructure
//create the IntPtr  

refParStruct rs = new RefPar();

IntPtr refparPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(refPar)));


Marshal.StructureToPtr(refParStruct, refparPtr,false);
ret=function(refParPtr);

Let Me know if you need more details

所谓喜欢 2024-11-01 19:11:27

调用此函数的另一种非常简单的方法是:

Create a c++ dll warapper that link your original dll and contains this function

//c++ code

function2(uint param1, uint param2.....)
{
  RefPar refpar;

  refpar.param1=param1

  refpar.param2=param2

  function(&refpar)

}



in this way you have just to import (in C#) the dll wrapper function in this way


[DllImport("wrapperdll.dll", EntryPoint = "function2"]
static extern int function2(Uint32 param1,Uint32 param2....);


that is very simple to call.


Regards

Another very simple way to call this function is:

Create a c++ dll warapper that link your original dll and contains this function

//c++ code

function2(uint param1, uint param2.....)
{
  RefPar refpar;

  refpar.param1=param1

  refpar.param2=param2

  function(&refpar)

}



in this way you have just to import (in C#) the dll wrapper function in this way


[DllImport("wrapperdll.dll", EntryPoint = "function2"]
static extern int function2(Uint32 param1,Uint32 param2....);


that is very simple to call.


Regards
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文