我必須將這個外部函數「GetOpenedFiles」(更多信息:http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx)用到我的C#應用程序中。 我不知道,因爲我可以寫這個功能的包裝:C#從C++更正託管代碼
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);
如何使用此dll函數corr真的在我的C#應用程序?
編輯:
這是我最新的片斷,但從來沒有調用回調函數:
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();
}
}
}
我認爲你的意思是在'委託'中的'IntPtr'作爲'UIntPtr',按照原始代碼以及可能在代碼末尾的右括號和分號。 :) +1雖然 – Maverik
感謝您的回覆。我怎樣才能將CallbackFunctionDef聲明爲我的回調函數? – CeccoCQ
任何具有相同簽名的方法都應該這樣做。例如:'void SomeCallback(OF_INFO_t info,UIntPtr context){/ * code here * /}' –