Komischer Linkererror
-
Hi Leute,
vorweg der Quelltext:#define _WIN32_WINNT 0x501 #include <windows.h> #include <psapi.h> #include <iostream> #include <vector> #include <string> #include <sstream> #include <fstream> using namespace std; class ProcessInfo { public: ProcessInfo(HANDLE handle, DWORD id, string name) { this->handle = handle; this->id = id; this->name = name; } BOOL WINAPI GetProcessHandleCount( HANDLE hProcess, PDWORD pdwHandleCount ); HANDLE getProcessHandle() {return handle;} DWORD getProcessId() {return id;} string getProcessName() {return name;} void terminate() { if (!handle) return; DWORD exitCode; if (GetExitCodeProcess(handle, &exitCode)) { TerminateProcess(handle, exitCode); } handle = 0; } void closeHandle() { if (!handle) return; CloseHandle(handle); handle = 0; } bool isRunning() { if (!handle) return false; DWORD handleCount; if (GetProcessHandleCount(handle, &handleCount)) { if (handleCount) return true; } handle = 0; return false; } static vector <ProcessInfo> getProcessInfos() { int arraySize = 128; DWORD *ids = new DWORD[arraySize]; DWORD idByteCount; vector <ProcessInfo> processInfos; while(EnumProcesses(ids, arraySize*sizeof(DWORD), &idByteCount)) { if (idByteCount < arraySize*sizeof(DWORD)) { for (int i = 0; i < idByteCount/sizeof(DWORD); i++) { HANDLE handle; if (handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ids[i])) { char buf[256]; if (!GetModuleBaseName(handle, 0, buf, sizeof(buf))) strcpy(buf, "<unknown>"); processInfos.push_back(ProcessInfo(handle, ids[i], buf)); } } break; } else { delete [] ids; arraySize *= 2; ids = new DWORD[arraySize]; } } delete [] ids; return processInfos; } static string makeProcessInfosString(vector <ProcessInfo> processInfos) { stringstream ss; ss << processInfos.size() << " processes:\n" << endl; for (int i = 0; i < processInfos.size(); i++) { ss << (processInfos[i].isRunning()?"running, ":" ") << "id=" << processInfos[i].getProcessId() << ", handle=" << (DWORD)processInfos[i].getProcessHandle() << ", name=\"" << processInfos[i].getProcessName() << "\"" << endl; } return ss.str(); } private: string name; HANDLE handle; DWORD id; }; string getLocalTimeString() { SYSTEMTIME sysTime; GetLocalTime(&sysTime); stringstream ss; ss << (sysTime.wHour>=10?"":"0") << sysTime.wHour << ':' << (sysTime.wMinute>=10?"":"0") << sysTime.wMinute << ':' << (sysTime.wSecond>=10?"":"0") << sysTime.wSecond << ", " << sysTime.wDay << '.' << sysTime.wMonth << '.' << sysTime.wYear; return ss.str(); } int main() { fstream fs; fs.open("c:\\processes.txt", ios_base::out | ios_base::trunc); if (!fs.is_open()) return 0; fs << getLocalTimeString() << endl; fs << ProcessInfo::makeProcessInfosString(ProcessInfo::getProcessInfos()); fs.close(); return 0; }Fehler:
C:\DOKUME1\x\LOKALE1\Temp\ccKYaaaa.o(.text\_ZN11ProcessInfo9isRunningEv[ProcessInfo::isRunning()]+0x30) In function `ZNSt24\_\_copy\_backward\_dispatchIP11ProcessInfoS1\_12\_\_false\_typeE4copyES1\_S1\_S1\_': [Linker error] undefined reference to `_ZN11ProcessInfo21GetProcessHandleCountEPvPm@12' C:\\DOKUME~1\\x\\LOKALE~1\\Temp\\ccKYaaaa.o(.text_ZN11ProcessInfo9isRunningEv[ProcessInfo::isRunning()]+0x30) ld returned 1 exit statusIch benutzte Dev-C++ Compiler(4.9.9.2 Neuste Version), -lpsapi habe ich bei Compileroptionen eingebunden. Nun wüsste ich gerne wie ich diesen Fehler beheben könnte!
Arbeite unter Windows XP Home SP 2Danke an Alle für die Hilfe!
Gruß Twitch
-
Du hast die Funktion 'GetProcessHandleCount' als Klassenmember deklariert.
Da du aber die WinAPI-Funktion aufrufen möchtest, laß die Deklaration einfach weg (da du ja schon <windows.h> inkludierst hast).