0

i want to embed a resource in a exe file using c#.

If i use c++ code it works well :

UpdateResource(hResource,RT_RCDATA,MAKEINTRESOURCE(104),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPVOID)text,FileSize);

c# code that i use :

 IntPtr handle = BeginUpdateResource(this.NomeFileCryptato, false);
 IntPtr fileptr = ToPtr(encrypted);
 bool res = UpdateResource(handle, "RT_RCDATA", "104", 1040, fileptr,     Convert.ToUInt32(encrypted.Length));
 EndUpdateResource(handle, false);

Actualy, the c# code embed the resource in the exe file (let's call it a.exe) but if i embed the resource with c++, a.exe can read and extract, if i embed from c#, a.exe cannot.

any ideas?


this is the declaration for update resource in c# :

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

lpType and lpName are both strings and if i use UpdateResource(handle, "RT_RCDATA", "104", 1040, fileptr, Convert.ToUInt32(encrypted.Length)); the UpdateResource add the resource correctly to the exe.

The problem was on the c++. To reach the resource added from c# i have to use LPCSTR without the use of makeintresource macro.

LPCSTR nome = "CDATA";
LPCSTR tipo = "104";
hLibrary = LoadLibrary(this->filename);
hResource = FindResource(hLibrary, tipo, nome);

Thanks again for your time!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Valentino
  • 59
  • 1
  • 11
  • _'c# code that dosent work'_ is a bit too vague may be. That could be cause of the downvote. You might want to [**elaborate**](http://stackoverflow.com/posts/23986108/edit) on your particular problems. – πάντα ῥεῖ Jun 02 '14 at 01:19

1 Answers1

1

You appear to be passing incorrect values, and since they're not shown, I assume your functions (ToPtr, BeginUpdateResource, UpdateResource, and EndUpdateResource) may be incorrectly defined as well.

Note that MAKEINTRESOURCE(104) casts the integer value 104 to a string pointer; it does not create a string with value "104". Similarly RT_RCDATA is probably a macro for a value other than "RT_RCDATA", such as MAKEINTRESOURCE(10).

So start from the beginning. What does your declaration of UpdateResource look like? I quickly found two contradictory examples:

The first makes it hard to handle integer IDs; the second makes it hard to handle strings. I guess I would expect to use IntPtr for both the Type and Name parameters.

Once you sort that out, the next bit is understanding what the MAKEINTRESOURCE macro's equivalent is in C#. And that requires understanding how MAKEINTRESOURCE smuggles integers in pointers. Once you understand that, it's easy to tell what you need to pass here to pass the integers 104 or 10 into UpdateResource.

Walkman
  • 67
  • 1
  • 2
  • 8
Michael Urman
  • 15,737
  • 2
  • 28
  • 44
  • That is the part that confuse me because there isnt a makeintresource macro in c#. Yes, The RT_RCDATA is a macro for makeintresource(10), how can i call UpdateResource from c# correctly? – Valentino Jun 02 '14 at 09:21
  • @user3374388 See my updates; let me know what's still confusing. – Michael Urman Jun 02 '14 at 11:52
  • hi and thanks for your time. This is the declaration for updateresource in c# : [DllImport("kernel32.dll", SetLastError = true)] static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData); – Valentino Jun 03 '14 at 07:15