GetFullPathName wont get DLL Path

-1

I am trying to load a DLL from resource and use SetWindowsHook to inject DLL to all Process GetFullPathName doesnt Seem to Work in this case, Now i am asking what would I do to get the DLL Path in this case, My code looks like this. I am new to using This and hence i cannot seem to get the DLL Path

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "resource.h"

void ExtractnRun()
{
    char* name = getenv("USERNAME");
    char info[1500];
    char aNewFile[1500];
    sprintf(info,"C:\\Users\\%s\\AppData\\Local\\MicroSoftX",name);
    //_mkdir(info);
    if (CreateDirectoryA(info, NULL)) 
    {
        MessageBoxA(NULL, "Directory Created", "", MB_OK);
    }
    // Extract From Resource 

    HRSRC hrsrc = FindResource(0, MAKEINTRESOURCE(IDR_DLL21),"DLL2");
    DWORD size = SizeofResource(0, hrsrc);
    PVOID buff = LockResource(LoadResource(0, hrsrc));

    DWORD dwBytesToWrite = (DWORD)strlen((char*)buff);
    DWORD dwBytesWritten = 0;

    sprintf(aNewFile, "C:\\Users\\%s\\AppData\\Local\\MicroSoftX\\mshelp.dll", name);

    HANDLE hFile = CreateFileA(aNewFile, GENERIC_WRITE, 0, NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile) 
    {
        MessageBoxA(NULL, "File Created!", "", MB_OK);
    }
    /*FILE* f = fopen(aNewFile, "wb");
    fwrite(buff,1,size,f);
    fclose(f);
    */
    if (WriteFile(hFile, buff, size, &dwBytesWritten, NULL)) 
    {
        MessageBoxA(NULL, "Data Written to DLL", "", MB_OK);
    }

    /*STARTUPINFOA si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    */

    char dll[MAX_PATH];
    GetFullPathName((LPCSTR)hFile, MAX_PATH, dll, NULL); // Shows Error here Cannot get Full Path of DLL

    printf("%s\n",dll);

    HMODULE MYdll = LoadLibrary(dll);
    if (MYdll == NULL)
    {
        printf("dll cannot be found!\n");
        getchar();
        printf("DLL : %s", MYdll);
    }

    HOOKPROC addr = (HOOKPROC)GetProcAddress(MYdll, "SayHelloWorld");
    if (addr == NULL)
    {
        printf("Cannot find Address!\n");
        getchar();
    }

    HHOOK handle = SetWindowsHookEx(WH_KEYBOARD, addr, MYdll, 0);
    if (handle == NULL)
    {
        printf("Hook Failed!\n");
        getchar();
    }

    printf("Program Hooked!\n");
    getchar();

    UnhookWindowsHookEx(handle);
    //printf("%s\n",dll);
    system("PAUSE");
}


int main()
{
    ExtractnRun();
    return 0;
}

The Exception Error i get looks like this :

Exception thrown at 0x7764171A (ntdll.dll) in ResourceExample.exe: 0xC0000005: Access violation reading location 0x0000009C.

If there is a handler for this exception, the program may be safely continued.

What am I not getting correctly?

c++
resources
asked on Stack Overflow Mar 6, 2018 by TimTim

1 Answer

0

You can not pass the file handler to the "GetFullPahtName". It should be file name to find full path.

GetFullPathName((LPCSTR)hFile, MAX_PATH, dll, NULL);

Please refer the below link for more details.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364963(v=vs.85).aspx

answered on Stack Overflow Mar 6, 2018 by Manivasakan

User contributions licensed under CC BY-SA 3.0