[英]C# Correct managed code from C++
我必須在我的C#應用程序中使用此外部函數“ GetOpenedFiles”(更多信息,請訪問: http : //www.codeproject.com/KB/shell/OpenedFileFinder.aspx )。 我不知道可以寫此函數的包裝器:
void GetOpenedFiles(LPCWSTR lpPath, OF_TYPE Filter, OF_CALLBACK CallBackProc, UINT_PTR pUserContext);
原始C ++代碼(OpenFilefinder.h)
enum OF_TYPE
{
FILES_ONLY = 1,
MODULES_ONLY = 2,
ALL_TYPES = 3
};
struct OF_INFO_t
{
DWORD dwPID;
LPCWSTR lpFile;
HANDLE hFile;
};
typedef void (CALLBACK* OF_CALLBACK)(OF_INFO_t OpenedFileInf0, UINT_PTR uUserContext );
extern "C" __declspec(dllexport) void ShowOpenedFiles( LPCWSTR lpPath );
extern "C" __declspec(dllexport) void GetOpenedFiles( LPCWSTR lpPath,
OF_TYPE Filter,
OF_CALLBACK CallBackProc,
UINT_PTR pUserContext );
我的C#應用程序:
public enum OF_TYPE : int
{
FILES_ONLY = 1,
MODULES_ONLY = 2,
ALL_TYPES = 3
}
public struct OF_INFO_t
{
?????? dwPID;
?????? lpFile;
?????? hFile;
}
[DllImport("OpenFileFinder.dll", EntryPoint = "GetOpenedFiles")]
static extern void GetOpenedFiles(??????? lpPath, OF_TYPE filter, ????? CallBackProc, ????? pUserContext);
如何在C#應用程序中正確使用此dll函數?
編輯:
這是我的最新代碼段,但從不調用回調函數:
namespace Open64
{
class Program
{
public Program()
{
GetOpenedFiles("C:\\", OF_TYPE.ALL_TYPES, CallbackFunction, UIntPtr.Zero);
}
//void GetOpenedFiles(LPCWSTR lpPath, OF_TYPE Filter, OF_CALLBACK CallBackProc, UINT_PTR pUserContext);
public enum OF_TYPE : int
{
FILES_ONLY = 1,
MODULES_ONLY = 2,
ALL_TYPES = 3
}
public struct OF_INFO_t
{
Int32 dwPID;
String lpFile;
IntPtr hFile;
}
public delegate void CallbackFunctionDef(OF_INFO_t info, IntPtr context);
[DllImport("OpenFileFinder.dll", EntryPoint = "GetOpenedFiles")]
static extern void GetOpenedFiles(string lpPath, OF_TYPE filter, CallbackFunctionDef CallBackProc, UIntPtr pUserContext);
public void CallbackFunction(OF_INFO_t info, IntPtr context)
{
Console.WriteLine("asd");
}
[STAThread]
static void Main()
{
new Program();
}
}
}
這就是您封送以下類型的方式:
DWORD => Int32
LPCWSTR => String
HANDLE => IntPtr
UINT_PTR => UIntPtr
public struct OF_INFO_t
{
Int32 dwPID;
String lpFile;
IntPtr hFile;
}
public delegate void CallbackFunctionDef(OF_INFO_t info, UIntPtr context);
[DllImport("OpenFileFinder.dll", EntryPoint = "GetOpenedFiles")]
static extern void GetOpenedFiles(string lpPath, OF_TYPE filter, CallbackFunctionDef CallBackProc, UIntPtr pUserContext);
編輯:這是完整的程序
class Program
{
public Program()
{
GetOpenedFiles("C:\\", OF_TYPE.ALL_TYPES, CallbackFunction, UIntPtr.Zero);
Console.ReadKey();
}
//void GetOpenedFiles(LPCWSTR lpPath, OF_TYPE Filter, OF_CALLBACK CallBackProc, UINT_PTR pUserContext);
public enum OF_TYPE : int
{
FILES_ONLY = 1,
MODULES_ONLY = 2,
ALL_TYPES = 3
}
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
public struct OF_INFO_t
{
public Int32 dwPID;
public String lpFile;
public IntPtr hFile;
}
public delegate void CallbackFunctionDef(OF_INFO_t info, IntPtr context);
[DllImport("OpenFileFinder.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "GetOpenedFiles")]
static extern void GetOpenedFiles(string lpPath, OF_TYPE filter, CallbackFunctionDef CallBackProc, UIntPtr pUserContext);
public void CallbackFunction(OF_INFO_t info, IntPtr context)
{
//List the files
Console.WriteLine(info.lpFile);
}
[STAThread]
static void Main()
{
new Program();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.