473,467 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using UpdateResource() with PInvoke

hi,
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?

Nov 17 '05 #1
10 7886
> [DllImport("kernel32.dll", SetLastError=true)]
static extern bool UpdateResource(IntPtr hUpdate, String lpType,
String lpName,
ushort wLanguage, IntPtr lpData, uint cbData);


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.
Nov 17 '05 #2
this solved the exception, but the file is not added to the file. are
you sure this is correct? the byte array is managed memory, which can
be moved around by the garbage collector, while the API is unmanaged
and normally expects a pointer to a buffer.

Nov 17 '05 #3
i even tried this:

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

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

where the lpData is exactly what it is in the API, and it doesn't work.
i have a similar problem elswhere, where i try to remove a resource by
sending null to lpData, and it doesn't work either.

Nov 17 '05 #4
Don't know if it's exactly what you want, but here's how I add a file as a
Win32 Resource:
[DllImport("kernel32", CharSet=CharSet.Unicode)]
private static extern IntPtr BeginUpdateResource(String pFileName, bool
bDeleteExistingResources);

[DllImport("kernel32", CharSet=CharSet.Unicode, SetLastError=true)]
private static extern int UpdateResource(IntPtr hUpdate,
System.Text.StringBuilder lpType, System.Text.StringBuilder lpName, int16
wLanguage, Byte[] lpData, int cbData);

[DllImport("kernel32", SetLastError=true)]
private static extern int EndUpdateResource(IntPtr hUpdate, bool fDiscard);
private void someMethod()
{
IntPtr hResource = BeginUpdateResource(@"..\123.dll", false);

//I have only been able to get UpperCase strings to work.
//If the strings are not uppercase then the resources will
//be replaced instead of added.

//UpperCase String.
System.Text.StringBuilder resType =
new System.Text.StringBuilder("FILE");

//UpperCase String.
System.Text.StringBuilder resName =
new System.Text.StringBuilder("Test.txt".ToUpper());

System.IO.FileStream someFileStream =
new IO.FileStream("..\\Test.txt", IO.FileMode.Open);

System.IO.BinaryReader fileReader =
new System.IO.BinaryReader(someFileStream);

Byte[] someByteArray =
fileReader.ReadBytes((int)someFileStream.Length);

UpdateResource(hResource, resType, resName, 1033, someByteArray,
someByteArray.Length);

EndUpdateResource(hResource, false);

fileReader.Close();
someFileStream.Close();

}

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
<ts********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
i even tried this:

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

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

where the lpData is exactly what it is in the API, and it doesn't work.
i have a similar problem elswhere, where i try to remove a resource by
sending null to lpData, and it doesn't work either.

Nov 17 '05 #5
i noticed a funny thing: every time i try running UpdateResource() on
the executable, i get a file named RCXxxx.tmp at the same folder as the
executable, with "xxx" being a random alphanumeric 3-characters string,
about the same size as the executable. when i rename it to .exe, i find
that the changes i did (add a module, or remove a module by sending
null to lpData) are in this file instead of the original file. what's
that?

i use BinaryReader.Read(byte[], int, int) because i need the data to
start at a certain offset down the buffer, while writing other data at
the at the first 8 bytes (i don't just add a file. i add the file
zip-compressed, so i have to write the compressed and uncompressed size
into the start of the buffer, so i can uncompress it).

Nov 17 '05 #6
i.e. when i use byte[] for lpData.

Nov 17 '05 #7
i noticed a funny thing: every time i try running UpdateResource() on
the executable, i get a file named RCXxxx.tmp at the same folder as the
executable, with "xxx" being a random alphanumeric 3-characters string,
about the same size as the executable. when i rename it to .exe, i find
that the changes i did (add a module, or remove a module by sending
null to lpData) are in this file instead of the original file. what's
that?

i use BinaryReader.Read(byte[], int, int) because i need the data to
start at a certain offset down the buffer, while writing other data at
the at the first 8 bytes (i don't just add a file. i add the file
zip-compressed, so i have to write the compressed and uncompressed size
into the start of the buffer, so i can uncompress it).

Nov 17 '05 #8
i.e. when i use byte[] for lpData.

Nov 17 '05 #9
well, i fixed the RCXxxx.tmp thing - it was because the .exe was locked
with LoadLibrary() from another method and wasn't released
(FreeLibrary()). i would expect EndUpdateResource to return false in
this case, not create a tmp file :/

i will also try using the GCHandle.Alloc() method, it looks more
correct to me.

Nov 17 '05 #10
well, i fixed the RCXxxx.tmp thing - it was because the .exe was locked
with LoadLibrary() from another method and wasn't released
(FreeLibrary()). i would expect EndUpdateResource to return false in
this case, not create a tmp file :/

i will also try using the GCHandle.Alloc() method, it looks more
correct to me.

Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Dyer | last post by:
We need to be able to set the version information for .net exe's and dll's after the build has happened. This is so we do not need to check out files that have not changed just to "tweak" the...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
2
by: Craig | last post by:
I've seen many examples of how to call SHGetFileInfo in shell32.dll to get a files associated icon, but I can't find anywhere how to get the file information (size, last date modified, etc, etc)...
6
by: Klaatu | last post by:
I've seen plenty of examples of updating a resource using UpdateResource(). Has anyone actually used it to add a new resource? For example, a menu resource. If so, how is it done? I'm not quit...
14
by: Nak | last post by:
Hi there, It's probably me being weird more than the function but I'm having problems with it doing as it should. I have a C++ application with 2 resources of custom types, RT_INIFILE @...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.