2011-07-27 42 views
2

我必須將這個外部函數「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(); 
     } 
    } 

} 

回答

2
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(); 
    } 
} 
+0

我認爲你的意思是在'委託'中的'IntPtr'作爲'UIntPtr',按照原始代碼以及可能在代碼末尾的右括號和分號。 :) +1雖然 – Maverik

+0

感謝您的回覆。我怎樣才能將CallbackFunctionDef聲明爲我的回調函數? – CeccoCQ

+0

任何具有相同簽名的方法都應該這樣做。例如:'void SomeCallback(OF_INFO_t info,UIntPtr context){/ * code here * /}' –

3

這是你將如何元帥以下幾種類型:

DWORD => Int32 
LPCWSTR => String 
HANDLE => IntPtr 
UINT_PTR => UIntPtr