none
embed dll (no src code) into exe, but shouldn't dump dll to disk while exe running RRS feed

  • 问题

  • 1. There is a function void func1() in destDll.dll. I don't have any source code of the dll.

    2. I want create a project1.exe, which embed the destDll.dll(it means while exe runing, destDll.dll must not exsist).And it is not allowed that destDll.dll dump to a disk.Because I want the operator is not able to get the destDll.dll.

    3. I have embedded dll with "insert bmp to resource" and get the binary of dll from memory while exe running and it's the same as the origin.

    4. However, I don't know how to loadlibrary, GetProcAddress from memory.

    5. Any more, it's supported that dll_attach_main, global variable initing may run while LoadLibrary. And the data in memory while running and the data of dll in disk is different. So it should be useless to call the address of offset.

    Thank you for your help!

    #include <Windows.h>
    #include <tchar.h>
    #include <cstdio>
    #include "resource.h"
     
    int main() {
            HMODULE aHmodule = LoadLibrary("../release/destDll.dll");
            if (aHmodule) {
                    FARPROC aFarproc = GetProcAddress(aHmodule, "func1");
                    printf("aFarproc=%u\n", aFarproc);//success
            }
            HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_RDLL1), "Rdll");
            FARPROC aFarproc = GetProcAddress(HMODULE(hRsrc), "func1");
            printf("aFarproc=%u\n", aFarproc);//fail
                     
            if (hRsrc) {
                    HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
                    FARPROC aFarproc = GetProcAddress(HMODULE(hGlobal), "func1");
                    printf("aFarproc=%u\n", aFarproc);//fail
                    DWORD dwSize = SizeofResource(NULL, hRsrc);
                    LPVOID pBuffer = LockResource(hGlobal);
                    if (pBuffer) {
                            FILE *fp = fopen(_T("1.x"), _T("wb"));
                            DWORD szWrite = fwrite(pBuffer, sizeof(char), dwSize, fp);
                            if (dwSize == szWrite) {
                                    printf("write %u\n", szWrite);//1.x and destDll.dll are same
                            }
                            fclose(fp);
                    }
            }
            return 0;
    }


    2017年10月23日 7:14

答案

  • Hi sculida,

    感谢在MSDN论坛发帖。

    >>embed dll (no src code) into exe, but shouldn't dump dll to disk while exe running

    如果你是在运行exe的时候,使dll不存在,在我看来是不可能的。LoadLibrary只能用来将指定的模块加载到调用进程的地址空间中,并不影响其他进程再次使用LoadLibrary调用。

    我建议你使用LockFile函数来锁定指定的文件以进行调用进程的独占访问。或者使用互斥对象保护共享资源免受多个线程或进程的同时访问。

    希望对你有所帮助。

    Best Regards,
    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已建议为答案 Baron Bi 2017年12月12日 7:14
    • 已标记为答案 sculida 2018年8月8日 2:12
    2017年10月24日 8:25