Sie verwenden wahrscheinlich den Namen main
für Ihre Funktion.
Abgesehen davon, vermissen Sie viele andere Funktionen. Sie müssen auch eine Bibliothek für GetModuleFileNameEx
hinzufügen. Es muss Windows-Bibliothek "psapi.lib
" oder "libpsapi.a
", wenn MinGW mit
win32: LIBS += -lpsapi
Beispiel:
#include <QCoreApplication>
#include <QString>
#include <QStringList>
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
#include <psapi.h>
QStringList foo()
{
QStringList list;
HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPMODULE, 0);
if (hndl)
{
PROCESSENTRY32 process;
memset(&process, 0, sizeof(PROCESSENTRY32));
process.dwSize = sizeof(PROCESSENTRY32);
Process32First(hndl, &process);
do
{
QString qpath;
wchar_t path[MAX_PATH] = L"";
HANDLE hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
if (hProcess)
{
GetModuleFileNameEx(hProcess, NULL, path, MAX_PATH);
qpath = QString::fromUtf16((const ushort*)path);
CloseHandle(hProcess);
}
else
{
qpath = QString::fromUtf16((const ushort*)process.szExeFile);
}
list.append(qpath);
} while (Process32Next(hndl, &process));
CloseHandle(hndl);
}
return list;
}
Nutzung:
QStringList list = foo();
for(int i = 0; i < list.count(); i++)
{
//print list[i]...
//or
std::wcout << (const wchar_t*)list[i].utf16() << std::endl;
}
das Programm im Admin-Modus laufen zu bekommen volle Pfad:
#include <iostream>
#include <windows.h>
#include <winbase.h>
#include <TlHelp32.h>
#include <psapi.h>
#include <Shlobj.h>
void test()
{
if (!IsUserAnAdmin())
std::wcout << "Run this in admin mode!\n";
void* tokenHandle;
TOKEN_PRIVILEGES privilegeToken;
LookupPrivilegeValue(0, SE_DEBUG_NAME, &privilegeToken.Privileges[0].Luid);
privilegeToken.PrivilegeCount = 1;
privilegeToken.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &tokenHandle);
AdjustTokenPrivileges(tokenHandle, 0, &privilegeToken, sizeof(TOKEN_PRIVILEGES), 0, 0);
CloseHandle(tokenHandle);
HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPMODULE, 0);
if (hndl)
{
PROCESSENTRY32 process;
memset(&process, 0, sizeof(PROCESSENTRY32));
process.dwSize = sizeof(PROCESSENTRY32);
Process32First(hndl, &process);
do
{
wchar_t path[MAX_PATH] = L"";
wcscpy_s(path, MAX_PATH, process.szExeFile);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process.th32ProcessID);
if (hProcess)
{
GetModuleFileNameExW(hProcess, 0, path, MAX_PATH);
CloseHandle(hProcess);
}
else
{
//in case it failed in WinXP
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, process.th32ProcessID);
if (hProcess)
{
GetModuleFileNameExW(hProcess, 0, path, MAX_PATH);
CloseHandle(hProcess);
}
}
std::wcout << path << std::endl;
} while (Process32Next(hndl, &process));
CloseHandle(hndl);
}
}
Mögliches Duplikat von [Was ist ein nicht definierter Verweis/ungelöster externer Symbolfehler und wie behebe ich ihn?] (Http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved- external-symbol-error-and-how-do-i-fix) – drescherjm
@drescherjm verstehe ich nicht? Können Sie mir im Detail Antwort geben, um das Problem zu lösen .... –