读书人

在C#里怎么获取VC++6.0生成资源文件的

发布时间: 2011-12-29 22:09:38 作者: rapoo

在C#里如何获取VC++6.0生成资源文件的DLL里的String Table
在C#里如何获取VC++6.0生成资源文件的DLL里的String Table ??
因为多国语言,之前C++的资源文件有相对应各国家的resources.
现在C#项目中需要用到resources的String Table 资源.C#调用C++的DLL需DllImport.那调用C++的资源DLL应该如何做??

[解决办法]
从资源文件里加载文件(C#)



用途:

1. 在进行多国语言化的时候.( 通常把不同翻译的文字存放到资源包里.)

2. 将图片打包进DLL的时候(icon等,自定义控件..)



资源文件格式: .resx||.resources

打包资源文件:

System.Resources.ResourceWriter rw = new ResourceWriter(@"..\..\abc.resources");

rw.AddResource("abc", new byte[10000000]);

rw.Generate();

rw.Close();

说明:

创建一个ResourceWriter的对象用于写资源文件。文件的后缀名必须是.resources,不让其它的系统无法识别。然后是通过AddResource的方法写资源信息,这个信息可以是一个字符串,也可以是一个byte数组,甚至可以是一个对象(image)。添加好以后就是Generate和close了。这样运行程序后,资源文件就制作完成了。

如果不需要把这个资源文件打包到exe程序或者dll程序里面,可以使用System.Resources.ResourceReader读取资源文件信息。



获取资源文件

资源文件打包好以后,下一步是把资源文件集成到项目里。方法有两个,一个是用控制台的方式编译项目,通过加载资源文件的方式把资源加载到exe里,另外一个简单很多,就是把直接把资源文件加载到工程里,vs会根据后缀名识别出来加载的文件属于资源文件,在进行编译的时候会自动把这个文件打包到资源文件里。如果编译后发现资源文件没有加到exe文件,可以设置资源文件的属性,把Build Action的属性设置为Embedded Resource。



在当前项目中读取资源:

this.Icon = Properties.Resources.exeIcon;||

this.Text = Resource1.String1.ToString();//主要是字符串



资源文件成功加到exe文件后,下一步就是在程序里读取资源文件。

System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Resunce.abc", this.GetType().Assembly);

byte[] bit = rm.GetObject("abc") as byte[];



说明:

通常是创建一个ResourceManager的对象,第一个参数是资源的名字,这里要注意的是“Resunce”是编译的时候,组件(命名空间)名字,“abc”是资源文件的名字。如果前面不是通过VS的工程文件进行编译,而是通过控制台方式直接编译,那么资源的名字不需要带组件的名字,只是“abc”就可以了。原因吗,我估计可能是vs在进行编译的时候,带了某些参数(确定了组件名),所以在项目读取资源文件的时候需要带组件名。


[解决办法]
从资源文件里加载文件或读取资源(C#)
比较详细了
[解决办法]
mark
[解决办法]

API

LoadLibrary
LoadResource
FreeLibrary
[解决办法]
关注学习
[解决办法]
--------------读资源--------------
using System;
using System.Resources;
using System.Collections;

public class ReadResources {

public static void Main(string[] args) {

// Opens a resource reader and gets an enumerator from it.
IResourceReader reader = new ResourceReader("myResources.resources");
IDictionaryEnumerator en = reader.GetEnumerator();

// Goes through the enumerator, printing out the key and value pairs.
while (en.MoveNext()) {
Console.WriteLine();
Console.WriteLine("Name: {0}", en.Key);
Console.WriteLine("Value: {0}", en.Value);
}
reader.Close();
}
}
------------写资源----------
using System;
using System.Resources;


public class WriteResources {
public static void Main(string[] args) {

// Creates a resource writer.
IResourceWriter writer = new ResourceWriter("myResources.resources");


// Adds resources to the resource writer.
writer.AddResource("String 1", "First String");

writer.AddResource("String 2", "Second String");

writer.AddResource("String 3", "Third String");

// Writes the resources to the file or stream, and closes it.
writer.Close();
}
}
下面连接看看
http://www.99inf.net/SoftwareDev/VC/53239.htm
[解决办法]
直接用Resources.resx添加不行吗?
[解决办法]
C++中使用
API

LoadLibrary
LoadResource
FreeLibrary
函数使用资源的方法你先弄会,然后用C#调用dll方式调用这些api,然后就和C++一样使用这些资源好了。
如果这些API不熟悉,就先研究这些C++中利用这些API使用资源的方法。
[解决办法]
有一段时间不用C++了,不能给你代码,但是从我以前使用的感觉,难度应该不高。
[解决办法]
测试可用

C# code
 

public static class Program{
[DllImport("kernel32.dll",EntryPoint="LoadLibraryA")]
public static extern IntPtr LoadLibrary(string sLibName);
[DllImport("kernel32.dll",EntryPoint="FreeLibrary")]
public static extern int FreeLibrary(IntPtr hLib);
[DllImport("user32.dll",EntryPoint="LoadStringA",CharSet = CharSet.Ansi)]
public static extern int LoadString (IntPtr hLib,uint resID,StringBuilder buf,int bufSize);

[STAThread]
static void Main(string [] args) {
IntPtr hDll = LoadLibrary(@"E:\LangChineseSimplified.dll");
if(hDll == IntPtr.Zero){
MessageBox.Show("Can't load library!");
return;
}
StringBuilder sb=new StringBuilder(1024);
int ret=0;
ret=LoadString(hDll,1,sb,1025);
if(ret == 0){
MessageBox.Show("String resource 1 is not found!");
FreeLibrary(hDll);
return;
}
MessageBox.Show("String Resource 100: " +sb.ToString());
FreeLibrary(hDll);
return;
}
}


[解决办法]
ret=LoadString(hDll,1,sb,1025);

这里的第二个参数是你的资源编号

如果乱码,请修改这个

[DllImport("user32.dll",EntryPoint="LoadStringA",CharSet = CharSet.Ansi)]
里面的CharSet

如果字符串很长,修改这个大小

StringBuilder sb=new StringBuilder(1024);

一般1024够用了

读书人网 >C#

热点推荐