如何使用p / invoke将C ++ DLL函数导入C#? [英] How can I import C++ DLL function using p/invoke into C#?

查看:115
本文介绍了如何使用p / invoke将C ++ DLL函数导入C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中有这个函数,它在头文件中定义



i have this function in c++ which defined in header file

MYDLL_API BOOL GetFolderSize(LPCTSTR lpszStartFolder, 
               BOOL bRecurse, 
               BOOL bQuickSize,
               PLARGE_INTEGER lpFolderSize,
               LPDWORD lpFolderCount /*= NULL*/,
               LPDWORD lpFileCount /*= NULL*/);





其中MYDLL_API定义为





where MYDLL_API is defined as

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







之后的com把它当作DLL项目

i为这个函数创建c#声明这个








after compile it as DLL project
i create c# declaration for this function as this


[StructLayout(LayoutKind.Explicit, Size = 8)]
    struct LARGE_INTEGER
    {
        [FieldOffset(0)]
        public Int64 QuadPart;
        [FieldOffset(0)]
        public UInt32 LowPart;
        [FieldOffset(4)]
        public Int32 HighPart;
    }

  [DllImport("mydll.dll")]
    extern static bool GetFolderSize(string lpszStartFolder,
                               bool bRecurse,
                               bool bQuickSize,
                               out LARGE_INTEGER lpFolderSize,
                                out UInt32 lpFolderCount,
                                out UInt32 lpFileCount);





在运行时它没有给出异常但它返回false(意味着函数失败)

为什么?是因为c ++中复杂的数据类型(PLARGE_INTEGER)

或者什么??

谢谢



我尝试过:



i试试这个



[StructLayout( LayoutKind.Explicit,Size = 8)]

struct LARGE_INTEGER

{

[FieldOffset(0)]

public Int64 QuadPart;

[FieldOffset(0)]

public UInt32 LowPart;

[FieldOffset(4)]

public Int32 HighPart;

}



[DllImport(mydll.dll)]

extern static bool GetFolderSize(string lpszStartFolder,

bool bRecurse,

bool bQuickSize,

out LARGE_INTEGER lpFolderSize,

out UInt32 lpFolderCount,

out UInt32 lpFileCount);



in run time it did not give an exception but it return false (mean that the function is failed )
why ? is the reason because of complicated data type in c++ (PLARGE_INTEGER)
or what ??
thanks

What I have tried:

i have try this

[StructLayout(LayoutKind.Explicit, Size = 8)]
struct LARGE_INTEGER
{
[FieldOffset(0)]
public Int64 QuadPart;
[FieldOffset(0)]
public UInt32 LowPart;
[FieldOffset(4)]
public Int32 HighPart;
}

[DllImport("mydll.dll")]
extern static bool GetFolderSize(string lpszStartFolder,
bool bRecurse,
bool bQuickSize,
out LARGE_INTEGER lpFolderSize,
out UInt32 lpFolderCount,
out UInt32 lpFileCount);

推荐答案

您正在调用的库正在使用 cdecl 调用约定,但C#的默认值是 winapi ,又名 stdcall 。改为你的 DllImport 调用看起来像这样:

The library you're calling is using cdecl calling convention, but the default for C# is winapi, a.k.a. stdcall. Change your DllImport call to look like this instead:
[DllImport("mydll.dll"), CallingConvention=CallingConvention.Cdecl)]
extern static bool GetFolderSize(string lpszStartFolder,
    bool bRecurse,
    bool bQuickSize,
    out LARGE_INTEGER lpFolderSize,
    out UInt32 lpFolderCount,
    out UInt32 lpFileCount);


0)确保您的方法原型正确无误。



1)将out参数更改为output。



2)当.Net提供完全相同的功能时,为什么要调用非托管DLL?
0) Make double-damn sure your method prototype is correct.

1) Change the "out" parameters to "output".

2) Why are you calling an unmanaged DLL to do that when .Net provides exactly the same functionality?


这篇关于如何使用p / invoke将C ++ DLL函数导入C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆