0

Hey I found a source code for my needs which accesses a .dll file with an ifstream and reads it with the following code:

std::ifstream File(@"C:\dllfile.dll", std::ios::binary | std::ios::ate);

if (File.fail()) {
    printf("Opening the file failed: %X\n", (DWORD)File.rdstate());
    File.close();
}

But I decided to read the dll file from the source code itself rather than streaming it from the disk.

To do that I've imported the .dll file into HxD, and exported it as C.

The exported source code of the dll looks like this:

char rawData[2785280] = { 0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, ... };

I've added it as dllcontent.h to my project, included the "dllcontent.h" inside main.cpp. Now I should be able to use the following code to achieve the same as above but with the char array:

std::ifstream File(rawData, std::ios::binary | std::ios::ate);

if (File.fail()) {
    printf("Opening the file failed: %X\n", (DWORD)File.rdstate());
    File.close();
    return -5;
}

But somehow it always triggers the File.fail check means it was not able to open the file?

How could I change the code that I get it working with content of the dll inside my project instead?

  • 2
    `@"C:\dllfile.dll"`? That's not C++. C++/CLI perhaps? – Some programmer dude Dec 20 '21 at 16:06
  • And the way you use `rawData`, it's considered a null-terminated string. You probably want `std::string` and `std::istringstream`. – Some programmer dude Dec 20 '21 at 16:08
  • @Someprogrammerdude I just changed it to make it more clear.. its wchar_t* dllPath; – Baseult Business Dec 20 '21 at 16:11
  • @Someprogrammerdude how should I use std::string for the dll? – Baseult Business Dec 20 '21 at 16:14
  • Put `rawData` into the `std::string` (or use a `std::string` to begin with rather than a `char` array). Then use it for the `std::istringstream` to read from the string as a file. If that's what you want to do. – Some programmer dude Dec 20 '21 at 16:20
  • @Someprogrammerdude I get an error for std::istringstream tho even If I use it like an example: std::string stringvalues = "125 320 512 750 333"; std::istringstream iss(stringvalues); error: an incomplete type is not allowed. The local variable was not initialized. – Baseult Business Dec 20 '21 at 16:23
  • `std::string rawData = { 0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, ... }; std::istringstream File(rawData, std::ios::binary);` should work fine. Assiming you include the proper header files... – Some programmer dude Dec 20 '21 at 16:30

0 Answers0