C#从C++更正托管代码
问题描述:
我必须将这个外部函数“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();
}
}
}
答
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();
}
}
答
这是你将如何元帅以下几种类型:
DWORD => Int32
LPCWSTR => String
HANDLE => IntPtr
UINT_PTR => UIntPtr
我认为你的意思是在'委托'中的'IntPtr'作为'UIntPtr',按照原始代码以及可能在代码末尾的右括号和分号。 :) +1虽然 – Maverik
感谢您的回复。我怎样才能将CallbackFunctionDef声明为我的回调函数? – CeccoCQ
任何具有相同签名的方法都应该这样做。例如:'void SomeCallback(OF_INFO_t info,UIntPtr context){/ * code here * /}' –