注入dll时链接到未解析的符号

时间:2013-12-21 19:18:09

标签: c++ winapi dll code-injection

我正在尝试将一个dll注入一个进程,但是在编译后我得到了 TestMain.obj : error LNK2019: link to unresolved external symbol __imp__StrStrIA@8 in function "unsigned long __cdecl GetPid(char *)" (?GetPid@@YAKPAD@Z)。经过多年的尝试在我的代码中找到我的问题,并试图谷歌,并与少林僧人进行冥想,我失败了。 这是我的代码:

#include <windows.h>
#include <stdio.h>
#include <tlhelp32.h>
#include <shlwapi.h>
#include <iostream>
#include <conio.h>
using namespace std;

#define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)

bool IsWindowsNT()
{
   // check current version of Windows
   DWORD version = GetVersion();
   // parse return
   DWORD majorVersion = (DWORD)(LOBYTE(LOWORD(version)));
   DWORD minorVersion = (DWORD)(HIBYTE(LOWORD(version)));
   return (version < 0x80000000);
}

BOOL InjectDLL(DWORD ProcessID,char* DLL_NAME)
{
   HANDLE Proc;
   char buf[50]={0};
   LPVOID RemoteString, LoadLibAddy;
   if(!ProcessID)
      return false;
   Proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
   LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
   RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_NAME), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
   WriteProcessMemory(Proc, (LPVOID)RemoteString, DLL_NAME,strlen(DLL_NAME), NULL);
   CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, (LPVOID)RemoteString, NULL, NULL);   
   CloseHandle(Proc);

   return true;
} 

DWORD GetPid(char *procName)
{
   PROCESSENTRY32 pe;
   HANDLE thSnapshot;
   BOOL retval, ProcFound = false;

   thSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   if(thSnapshot == INVALID_HANDLE_VALUE)
   {
       cout << "Error: unable to create toolhelp snapshot" << endl;
    //  MessageBox(NULL, "Error: unable to create toolhelp snapshot", "Loader", NULL);
      return false;
   }

   pe.dwSize = sizeof(PROCESSENTRY32);

   retval = Process32First(thSnapshot, &pe);

   while(retval)
   {
      if(StrStrI(pe.szExeFile, procName) )
      {
         ProcFound = true;
         break;
      }

      retval    = Process32Next(thSnapshot,&pe);
      pe.dwSize = sizeof(PROCESSENTRY32);
   }
   if (!ProcFound) return 0;
   return pe.th32ProcessID;
}

BOOL LoadDll(char *procName, char *dllName)
{
   DWORD ProcID = 0;

   ProcID = GetPid(procName);

   if(!(InjectDLL(ProcID, dllName)))
   {
       cout << "Process located, but injection failed" << endl;
       _getch();
        exit(1);
   }   //   MessageBox(NULL, "Process located, but injection failed", "Loader", NULL);
   else
   {
       cout << " Injection successfull!" << endl;
       _getch();
   }
   return true;
}


int main()
{
    char* ProcName = "notepad.exe";
    char* DllName = "Main.dll";
    LoadDll( ProcName, DllName );
    return 0;
}

我的dll:

#include <Windows.h>
DWORD APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
    switch( ul_reason_for_call )
    {
    case DLL_PROCESS_ATTACH:
    //  hInstance = (HINSTANCE) hModule;
        MessageBox( 0, "HOHOHOOHOHOHO!", "DLLHOOK", MB_OK );
        return TRUE;
    case DLL_PROCESS_DETACH:
        break;
    }   
    return TRUE;
}

所以主要问题是错误。谢谢回答。

1 个答案:

答案 0 :(得分:4)

尝试添加

#pragma comment (lib, 'Shlwapi.lib')

低于您的其他#includes