C# 动态添加DLL(动态连接库)

 

class Dll

{

/// <summary>
/// To load the dll - dllFilePath dosen't have to be const - so I can read path from registry
/// </summary>
/// <param name="dllFilePath">file path with file name</param>
/// <param name="hFile">use IntPtr.Zero</param>
/// <param name="dwFlags">What will happend during loading dll
/// <para>LOAD_LIBRARY_AS_DATAFILE</para>
/// <para>DONT_RESOLVE_DLL_REFERENCES</para>
/// <para>LOAD_WITH_ALTERED_SEARCH_PATH</para>
/// <para>LOAD_IGNORE_CODE_AUTHZ_LEVEL</para>
/// </param>
/// <returns>Pointer to loaded Dll</returns>
[
DllImport(kernel32)]
private static extern IntPtr LoadLibraryEx(string dllFilePath, IntPtr hFile, uint dwFlags);

///
 <summary>
/// To unload library 
/// </summary>
/// <param name="dllPointer">Pointer to Dll witch was returned from LoadLibraryEx</param>
/// <returns>If unloaded library was correct then true, else false</returns>
[DllImport(kernel32)]
public extern static bool FreeLibrary(IntPtr dllPointer);

/// <summary>
/// To get function pointer from loaded dll 
/// </summary>
/// <param name="dllPointer">Pointer to Dll witch was returned from LoadLibraryEx</param>
/// <param name="functionName">Function name with you want to call</param>
/// <returns>Pointer to function</returns>
[DllImport(kernel32, CharSet = CharSet.Ansi)]
public extern static IntPtr GetProcAddress(IntPtr dllPointer, string functionName);

/// <summary>
/// This will to load concret dll file
/// </summary>
/// <param name="dllFilePath">Dll file path</param>
/// <returns>Pointer to loaded dll</returns>
/// <exception cref="ApplicationException">
/// when loading dll will failure
/// </exception>
public static IntPtr LoadWin32Library(string dllFilePath)
{
    System.
IntPtr moduleHandle = LoadLibraryEx(dllFilePath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
    
if (moduleHandle == IntPtr.Zero)
    {
        
// I'm gettin last dll error
        
int errorCode = Marshal.GetLastWin32Error();
        
throw new ApplicationException(
            
string.Format("There was an error during dll loading : {0}, error - {1}", dllFilePath, errorCode)
            );
    }
    
return moduleHandle;
}

}

// **************************************************************************************************************************
// That is all, now how to use this functions
// **************************************************************************************************************************

static IntPtr myDll ;

public static void InitializeMyDll()
{
    
try
    
{
        myDll = 
Dll.LoadWin32Library("path to my dll with file path name");
        // here you can add, dl version check
    
}
    
catch (ApplicationException exc)
    {
        
MessageBox.Show(exc.Message, "There was an error during dll loading",MessageBoxButtons.OK,MessageBoxIcon.Error);
        throw exc;
    
}
}

// The last thing is to create delegate to calling function

// delegate must to have the same parameter then calling function (from dll)
public
 delegate RETURN_TYPE DllFunctionDelegate(int a, string b);
public static RETURN_TYPE functionName(int a, string b)
{
    if (myDll == IntPtr.Zero)
        InitializeMyDll();
    IntPtr pProc = Dll.GetProcAddress(myDll , "CallingFunctionNameFromCallingDllFile");
    DllFunctionDelegate dllFD = (DllFunctionDelegate)Marshal.GetDelegateForFunctionPointer(pProc,typeof(DllFunctionDelegate));
    // Now i'm calling delegate, with is calling function from dll file 

    return dllFD (nOper, hWnd, out dwData);
}

// Now if you want to call dll function from program code use this
// for ex. you want to call c++ function RETURN_TYPECallingFunctionNameFromCallingDllFile(int nIDLPSTR lpstrName);

RETURN_TYPE ret;
ret = functionName
(2, "name");

 

 

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/200e01d5-bc2b-4688-92af-570688636b7c


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值