Плз обьясните чайнику в чем разница работы с WinAPI компиляторов борланда и майкрософсткого вижуал студио.Вот к примеру такой код Код (Text): #include <stdio.h> #include <windows.h> typedef LONG KPRIORITY; typedef UINT NTSTATUS; typedef enum _SYSTEMINFOCLASS { SystemBasicInformation, // 0x002C SystemProcessorInformation, // 0x000C SystemPerformanceInformation, // 0x0138 SystemTimeInformation, // 0x0020 SystemPathInformation, // not implemented SystemProcessInformation, // 0x00C8+ per process SystemCallInformation, // 0x0018 + (n * 0x0004) SystemConfigurationInformation, // 0x0018 SystemProcessorCounters, // 0x0030 per cpu SystemGlobalFlag, // 0x0004 (fails if size != 4) SystemCallTimeInformation, // not implemented SystemModuleInformation, // 0x0004 + (n * 0x011C) SystemLockInformation, // 0x0004 + (n * 0x0024) SystemStackTraceInformation, // not implemented SystemPagedPoolInformation, // checked build only SystemNonPagedPoolInformation, // checked build only SystemHandleInformation, // 0x0004 + (n * 0x0010) SystemObjectTypeInformation, // 0x0038+ + (n * 0x0030+) SystemPageFileInformation, // 0x0018+ per page file SystemVdmInstemulInformation, // 0x0088 SystemVdmBopInformation, // invalid info class SystemCacheInformation, // 0x0024 SystemPoolTagInformation, // 0x0004 + (n * 0x001C) SystemInterruptInformation, // 0x0000, or 0x0018 per cpu SystemDpcInformation, // 0x0014 SystemFullMemoryInformation, // checked build only SystemLoadDriver, // 0x0018, set mode only SystemUnloadDriver, // 0x0004, set mode only SystemTimeAdjustmentInformation, // 0x000C, 0x0008 writeable SystemSummaryMemoryInformation, // checked build only SystemNextEventIdInformation, // checked build only SystemEventIdsInformation, // checked build only SystemCrashDumpInformation, // 0x0004 SystemExceptionInformation, // 0x0010 SystemCrashDumpStateInformation, // 0x0004 SystemDebuggerInformation, // 0x0002 SystemContextSwitchInformation, // 0x0030 SystemRegistryQuotaInformation, // 0x000C SystemAddDriver, // 0x0008, set mode only SystemPrioritySeparationInformation,// 0x0004, set mode only SystemPlugPlayBusInformation, // not implemented SystemDockInformation, // not implemented SystemPowerInfo, // 0x0060 (XP only!) SystemProcessorSpeedInformation, // 0x000C (XP only!) SystemTimeZoneInformation, // 0x00AC SystemLookasideInformation, // n * 0x0020 SystemSetTimeSlipEvent, SystemCreateSession, // set mode only SystemDeleteSession, // set mode only SystemInvalidInfoClass1, // invalid info class SystemRangeStartInformation, // 0x0004 (fails if size != 4) SystemVerifierInformation, SystemAddVerifier, SystemSessionProcessesInformation, // checked build only MaxSystemInfoClass } SYSTEMINFOCLASS, *PSYSTEMINFOCLASS; typedef enum { StateInitialized, StateReady, StateRunning, StateStandby, StateTerminated, StateWait, StateTransition, StateUnknown } THREAD_STATE; typedef enum _KWAIT_REASON { Executive, FreePage, PageIn, PoolAllocation, DelayExecution, Suspended, UserRequest, WrExecutive, WrFreePage, WrPageIn, WrPoolAllocation, WrDelayExecution, WrSuspended, WrUserRequest, WrEventPair, WrQueue, WrLpcReceive, WrLpcReply, WrVirtualMemory, WrPageOut, WrRendezvous, Spare2, Spare3, Spare4, Spare5, Spare6, WrKernel, MaximumWaitReason } KWAIT_REASON, *PKWAIT_REASON; typedef struct _CLIENT_ID { HANDLE UniqueProcess; HANDLE UniqueThread; } CLIENT_ID, *PCLIENT_ID; typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING, *PUNICODE_STRING; typedef struct _SYSTEM_THREAD { LARGE_INTEGER liKernelTime; // 100 nsec units LARGE_INTEGER liUserTime; // 100 nsec units LARGE_INTEGER liCreateTime; // relative to 01-01-1601 ULONG WaitTime; // ticks PVOID pStartAddress; // EIP CLIENT_ID Cid; // process/thread ids KPRIORITY Priority; KPRIORITY BasePriority; ULONG ContextSwitches; THREAD_STATE ThreadState; KWAIT_REASON WaitReason; // DWORD dwUnknown2; // maybe it not exists !!! } SYSTEM_THREAD, *PSYSTEM_THREAD; typedef struct _VM_COUNTERS { ULONG uPeakVirtualSize; ULONG uVirtualSize; ULONG uPageFaultCount; ULONG uPeakWorkingSetSize; ULONG uWorkingSetSize; ULONG uQuotaPeakPagedPoolUsage; ULONG uQuotaPagedPoolUsage; ULONG uQuotaPeakNonPagedPoolUsage; ULONG uQuotaNonPagedPoolUsage; ULONG uPagefileUsage; ULONG uPeakPagefileUsage; } VM_COUNTERS, *PVM_COUNTERS; typedef struct _IOCOUNTERS { ULONG uReadOperationCount; ULONG uWriteOperationCount; ULONG uOtherOperationCount; LARGE_INTEGER liReadTransferCount; LARGE_INTEGER liWriteTransferCount; LARGE_INTEGER liOtherTransferCount; } IOCOUNTERS, *PIOCOUNTERS; typedef struct _SYSTEM_PROCESS_INFORMATION { ULONG uNext; // relative offset ULONG uThreadCount; LARGE_INTEGER liUnknown1; LARGE_INTEGER liUnknown2; LARGE_INTEGER liUnknown3; LARGE_INTEGER liCreateTime; // relative to 01-01-1601 LARGE_INTEGER liUserTime; // 100 nsec units LARGE_INTEGER liKernelTime; // 100 nsec units UNICODE_STRING usName; KPRIORITY BasePriority; ULONG uUniqueProcessId; ULONG uInheritedFromUniqueProcessId; ULONG uHandleCount; ULONG uSessionId; // W2K Only! DWORD dwUnknown5; VM_COUNTERS VmCounters; ULONG uCommitCharge; // bytes SYSTEM_THREAD aST[]; } SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION; typedef NTSTATUS (NTAPI *PZwQuerySystemInformation)( SYSTEMINFOCLASS SystemInformationClass, PVOID pSystemInformation, ULONG uSystemInformationLength, PULONG puReturnLength ); int main(int argc, char** argv) { PZwQuerySystemInformation ZwQuerySystemInformation; PSYSTEM_PROCESS_INFORMATION SPI=NULL; ULONG memlen,bw; char PrcName[1024]; ZwQuerySystemInformation=GetProcAddress(GetModuleH andle("ntdll.dll"),"ZwQuerySystemInformation"); if(!ZwQuerySystemInformation)return -1; printf("PID\tThreads\t\tName\n\n");; ZwQuerySystemInformation(SystemProcessInformation, SPI,0,&memlen); SPI=VirtualAlloc(NULL,memlen,MEM_COMMIT,PAGE_READW RITE); if(!ZwQuerySystemInformation(SystemProcessInformat ion,SPI,memlen,&bw)) while(SPI->uNext>0) { SPI=(PVOID)(SPI->uNext+(ULONG)SPI); if((SPI->usName.Length / 2)>=sizeof(PrcName)-1)continue; wcstombs(PrcName,SPI->usName.Buffer,SPI->usName.Length); printf("%u\t%u\t\t%s\n",SPI->uUniqueProcessId,SPI->uThreadCount,PrcName); } VirtualFree(SPI,memlen,MEM_DECOMMIT); return 0; } работает только на майкрософстком кмпиляторе.А у борланда на этой строке Код (Text): ZwQuerySystemInformation=GetProcAddress(GetModuleHandle("ntdll.dll"),"ZwQuerySystemInformation"); Вылазит такая ругань Код (Text): [C++ Error] Unit1.h(404): E2034 Cannot convert 'int (__stdcall *)()' to 'unsigned int (__stdcall *)(_SYSTEMINFOCLASS,void *,unsigned long,unsigned long *)'
Это на каком, на VC6, что ли? Эта старая рухлядь не соответствует стандарту, даже его первой версии от 1998 года. Точно могу сказать, даже не проверяя, что такой код не скомпилится в VS2005/2008. Используй приведение типов: Код (Text): ZwQuerySystemInformation = (PZwQuerySystemInformation) GetProcAddress(GetModuleHandle("ntdll.dll"),"ZwQuerySystemInformation"); P.S. BTW, твой код вполне компилируемый, если его рассматривать как код на языке C, а не C++.
Ну под борландом этот код после таких изменений Код (Text): .................... (int(__stdcall*)())ZwQuerySystemInformation=GetProcAddress(GetModuleHandle("ntdll.dll"),"ZwQuerySystemInformation"); .................... (void*)SPI=VirtualAlloc(NULL,memlen,MEM_COMMIT,PAGE_READWRITE); .................... (void*)SPI=(PVOID)(SPI->uNext+(ULONG)SPI); заработал. ЗЫ:а с какой стуктуры можно выдрать имя пользователя от которого запущен процесс?
_nic Это знает винлогон, для вызывающего потока это advapi32.GetUserName(), там запрос к винлогон через порт "\LsaAuthenticationPort". Можно посмотреть сервисы этого порта, хотя должна быть соответствующая апи. [тьфу, не винлогон а lsass)]
пробую так Код (Text): char szUser[1024];DWORD pcchU,pcchD; char szDomain[1024]; HANDLE hToken = NULL; PTOKEN_USER ptiUser = NULL; DWORD cbti = 0; SID_NAME_USE snu; ................. SetLastError(0); HANDLE h=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,SPI->uUniqueProcessId); OpenProcessToken(h,TOKEN_QUERY,&hToken);ShowMessage(GetLastError()); GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti); ptiUser = (PTOKEN_USER)HeapAlloc(GetProcessHeap(),0,cbti); GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti); LookupAccountSid(NULL,ptiUser->User.Sid,szUser,&pcchU,szDomain,&pcchD,&snu); .................... Неработает Получаю код ошибки "5" тоесть "ERROR_ACCESS_DENIED" .Чего может нехватать??