问题描述
我有一个c ++函数,我使用pinInvoke从c#调用。以下是我的cpp方法 -
I have a c++ function which I am calling from c# using pinInvoke. Following is my cpp method-
int test(DWORD verb,DWORD verb2 )
{
return verb2 *100;
}
我的功能曝光为 -
My function is exposed as -
extern "C" {
__declspec(dllexport) int test(DWORD verb, DWORD verb2);
}
以下是我的c#代码,我在调用上述方法:
公共类API
{
[DllImport(mydll.dll,EntryPoint =test,SetLastError = true,CallingConvention = CallingConvention。 Cdecl)]
[返回:MarshalAs(UnmanagedType.U4)]
public static extern uint test(
[MarshalAs(UnmanagedType.U8) ] ulong动词,
[MarshalAs(UnmanagedType.U8)] ulong verb2
);
静态无效Main(string [] args)
{
uint x = DPAPI.test(26,10);
Console.Write(结果是 - + x);
}
}
这里第二个值传递为0 ,所以错误的结果即将到来。我在传递价值时做错了什么?
我尝试了什么:
我对Pinvoke相对较新。所以我尝试调试以查看值是否未传递给c ++代码或者c ++代码是否没有返回正确的值。我发现传递的值本身是错误的。
Following is my c# code where I am calling the above method:
public class API
{
[DllImport("mydll.dll", EntryPoint = "test", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U4)]
public static extern uint test(
[MarshalAs(UnmanagedType.U8)]ulong verb,
[MarshalAs(UnmanagedType.U8)]ulong verb2
);
static void Main(string[] args)
{
uint x = DPAPI.test(26,10);
Console.Write("result is-"+x);
}
}
Here the second value is getting passed as 0,so wrong result is coming.Am I doing something wrong while passing the value?
What I have tried:
I am relatively new to Pinvoke. So I tried debugging to see whether the value is not getting passed to c++ code or whether the c++ code is not returning proper values.I found that the value getting passed itself was wrong.
推荐答案
[MarshalAs(UnmanagedType.U8)] ulong动词,
[MarshalAs(UnmanagedType) .U8)] ulong verb2
[MarshalAs(UnmanagedType.U8)]ulong verb,
[MarshalAs(UnmanagedType.U8)]ulong verb2
应该是,而不是
That should be, instead
[MarshalAs(UnmanagedType.U4)] uint verb,
[MarshalAs(UnmanagedType.U4)] uint verb2
或者,简单地说:
or, simply:
uint verb,
uint verb2
这篇关于在pininvoke期间没有传递给C ++函数的第二个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!