English 中文(简体)
从C#代码到C++ DLL
原标题:Argument passed incorrectly from C# code to C++ DLL

我使用了以下数字:

[DllImport(@"someDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern UINT64 someFunc(int arga, int argb, int argc);

I m 称其职能如下:

someFunc(0,0,1);

档案中:

extern "C" __declspec(dllexport) UINT64 someFunc(int arga, int argb, int argc);

cpp:

UINT64 someFunc(int arga, int argb, int argc)
{
   ...
}

In the C++ code I receive weird values (such as 1218628, 20140292, 1219020).

任何想法为什么?

问题回答

You didn t show the C++ code so I don t see the problem in your code. So, I tried recreating it myself. I created a C# WPF project which calls into a DLL.

C#:

        [DllImport(@"c:usersownerdocumentsvisual studio 2010ProjectsMyDllReleaseMyDll.dll",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern UInt64 someFunc(int arga, int argb, int argc); 

        private void DoIt_Click(object sender, RoutedEventArgs e)
        {
            UInt64 val = someFunc(0, 0, 1);
            ResultLabel.Content = val.ToString();
        }

C++ DLL:

extern "C" __declspec(dllexport) unsigned __int64 someFunc(int arga, int argb, int argc)
{
    CString s;
    s.Format(L"%d	%d	%d", arga, argb, argc);
    AfxMessageBox(s);
    return arga + argb + argc;
}

C++的电文箱显示,预计01份,C#代码按预期恢复1份。

如果你能够,在___次电话中,使用__std打字器而不是__cdecl,则使用CL,进行掩蔽清理,是使用C车窗的标准方法。

It is also a good behaviour to specify the calling convention in your C code, both for readability and because C++ project settings can specify what calling convention use by default.

如要明确确定第__cdecl号,或许是第++号汇编员正在使用第__号招贴汇编这一文件。

extern "C" __declspec(dllexport) UINT64 __cdecl someFunc(int arga, int argb, int argc);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签