get string from win32 dll in .NET

来源:互联网 发布:ubuntu系统无法上网 编辑:程序博客网 时间:2024/05/17 03:41

using System;

using System.Runtime.InteropServices;

 

[DllImport("kernel32")]

publicstaticexternIntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32")]

publicstaticexternBoolean FreeLibrary(IntPtr hResModule);

 [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "LoadStringW", ExactSpelling = true)]

publicstaticexternint LoadString(

IntPtr hInstance,

uint uID,

StringBuilder lpBuffer,

int nBufferMax);

 

publicstaticString GetStringResource(IntPtr hModuleInstance, uint uiStringID)

{

StringBuilder sb = newStringBuilder(255);

            LoadString(hModuleInstance, uiStringID, sb, sb.Capacity + 1);

return sb.ToString();

}

public staticstring GetWin32Resource(string componentName, uint uiResourceId)

{

            string str = string.Empty;

            IntPtr hMod = LoadLibrary(@"***\test.dll");

     if (hMod !=null)

            {

                str = GetStringResource(hMod, uiResourceId);

                FreeLibrary(hMod);

            }

     return str;

}