本文介绍了使用带有PInvoke的UpdateResource()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用Win32 API UpdateResource函数将一个文件

添加到exe文件中,但不知怎的,我得到了一个NullReferenceException当我用
调用该函数时。这是一段代码:


GCHandle gch = GCHandle.Alloc(fileBuffComp);

if(UpdateResource(hResource," FILE",fileName) ,MakeLangId(0x09,

0x01),

/ * * /(IntPtr)gch,compFileLen)== false){

throw new Win32Exception(Marshal.GetLastWin32Error());

}


其中fileBuffComp是包含该文件的字节数组,在我读完之后
$ b来自磁盘的$ b(并使用#ZipLib压缩它)。该函数被声明为

,如下所示:


[DllImport(" kernel32.dll",SetLastError = true)]

static extern bool UpdateResource(IntPtr hUpdate,String lpType,

String lpName,

ushort wLanguage,IntPtr lpData,uint cbData);


任何想法?


i''m trying to use the Win32 API UpdateResource function to add a file
to an exe file, but somehow i''m getting a NullReferenceException when i
call the function. here is a piece of the code:

GCHandle gch = GCHandle.Alloc(fileBuffComp);
if (UpdateResource(hResource, "FILE", fileName, MakeLangId(0x09,
0x01),
/* */(IntPtr)gch, compFileLen) == false) {
throw new Win32Exception(Marshal.GetLastWin32Error());
}

where fileBuffComp is a byte array containing the file, after i read it
from disk (and compressed it with #ZipLib). the function is declared
like this:

[DllImport("kernel32.dll", SetLastError=true)]
static extern bool UpdateResource(IntPtr hUpdate, String lpType,
String lpName,
ushort wLanguage, IntPtr lpData, uint cbData);

any ideas?

推荐答案




将此更改为


.... ushort wLanguage,byte [] lpData,uint cbData );


然后直接传入fileBuffComp,摆脱GCHandle。

Mattias


- -

Mattias Sj?gren [MVP] mattias @ mvps.org
|

请回复到新闻组。



Change this to

.... ushort wLanguage, byte[] lpData, uint cbData);

and then pass in fileBuffComp directly, and get rid of the GCHandle.
Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.




这篇关于使用带有PInvoke的UpdateResource()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 22:32