【发布时间】:2014-06-13 07:49:30
【问题描述】:
我有 MFC DLL 的问题。我使用 Visual Studio 2010。
这是我的 DLL
#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif
__declspec(dllexport) void __cdecl GetRAMInfo(DWORD& totaRamlPhysical, DWORD& availablephys, DWORD& memoload)
{
// Lay RAM
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status ); // lay information of ram
totaRamlPhysical = status.dwTotalPhys; // Tong dung luong RAM vat ly
availablephys = status.dwAvailPhys; // Dung luong RAM vat ly dang duoc su dug
memoload = status.dwMemoryLoad; // phan tram RAM duoc ca he thong su dung
}
__declspec(dllexport) void __cdecl GetCPUInfo(DWORD& processorArchitect, DWORD& typeProcessor, DWORD& numberProcessor)
{
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo); // lay thong tin cua CPU
processorArchitect = siSysInfo.wProcessorArchitecture;
numberProcessor = siSysInfo.dwNumberOfProcessors;
typeProcessor = siSysInfo.dwProcessorType;
}
__declspec(dllexport) void __cdecl GetRAMuseandProcessMostUse(CString& namePMU, DWORD& ramUse)
{
//===========NAME PMU ==========//
DWORD sizeallProcess; // Kich thuoc danh sach ID Process thu duoc
DWORD processNumber; // So luong Process thu duoc
DWORD arrayProcessID[100]; // mang chua danh sach ID Process
EnumProcesses( arrayProcessID, sizeof(arrayProcessID), &sizeallProcess ); // lay danh sach process dang chay
processNumber = sizeallProcess / sizeof(DWORD); // Lay so luong Process thu duoc
namePMU = GetProcess(arrayProcessID, processNumber);
///===========RAM USE==============//
DWORD arrayWorkingSetSize[100];
unsigned int i;
ramUse =0;
for( i =0; i < processNumber;i++){
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, arrayProcessID[i] ); // handle toi 1 process
if (hProcess != NULL)
{
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ){ // laays kich thuoc bo nho ma tien trinh do su dung
arrayWorkingSetSize[i] = pmc.WorkingSetSize ;
}
}
CloseHandle( hProcess );
}
for(i =2; i<(processNumber); i++){
if(arrayWorkingSetSize[i] != 3435973836){
ramUse += arrayWorkingSetSize[i];
}
}
}
__declspec(dllexport) void __cdecl GetCPUuseandProcessMostUse(CString& namePMUCPU, DOUBLE& cpuUse)
{
//DWORD processID = 2412;
FILETIME ftSysIdle, ftSysKernel, ftSysUser;
FILETIME ftProcCreation, ftProcExit, ftProcKernel, ftProcUser;
//=== HANDLE of a PROCESS ==///
DWORD sizeallProcess; // Kich thuoc danh sach ID Process thu duoc
DWORD processNumber; // So luong Process thu duoc
DWORD arrayProcessID[100]; // mang chua danh sach ID Process
EnumProcesses( arrayProcessID, sizeof(arrayProcessID), &sizeallProcess ); // lay danh sach process dang chay
processNumber = sizeallProcess / sizeof(DWORD); // Lay so luong Process thu duoc
bool flagFor = FALSE;
ULONGLONG arrayTotalSys[2][100];
ULONGLONG arrayTotalProc[2][100];
DOUBLE arrayPercent[100];
for(unsigned int a = 0; a<2; a++) {
for(unsigned int i=2; i<processNumber; i++) {
if(arrayProcessID[i] !=3435973836){
HANDLE hProcess;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, arrayProcessID[i] );
GetSystemTimes(&ftSysIdle, &ftSysKernel, &ftSysUser);
GetProcessTimes(hProcess, &ftProcCreation, &ftProcExit, &ftProcKernel, &ftProcUser);
arrayTotalSys[a][i] = AddTimes(ftSysKernel, ftSysUser);
arrayTotalProc[a][i] = AddTimes(ftProcKernel, ftProcUser);
}
}
Sleep(500);
}
DOUBLE sumSys =0;
DOUBLE sumProc =0;
for(unsigned int i=2; i<processNumber; i++){
if(arrayProcessID[i] !=3435973836){
/*sumSys += arrayTotalSys[0][i] + arrayTotalSys[1][i];
sumProc += arrayTotalProc[0][i] + arrayTotalProc[1][i];*/
sumSys += arrayTotalSys[1][i] - arrayTotalSys[0][i] ;
sumProc += arrayTotalProc[1][i] - arrayTotalProc[0][i] ;
arrayPercent[i] = (double)((100*sumProc)/sumSys);
}
}
int maxindex = 2;
double processMaxPercent = arrayPercent[2];
double sumpercent =0;
for(unsigned int i =2; i<(processNumber); i++){
if(arrayPercent[i] != 3435973836){
if(processMaxPercent < arrayPercent[i] ){
processMaxPercent = arrayPercent[i];
maxindex = i;
}
sumpercent += arrayPercent[i];
}
}
cpuUse = sumpercent;
namePMUCPU = GetNameProcessMU(arrayProcessID[maxindex]); // tam thoi lay ten mac djnh la cai nay
}
__declspec(dllexport) ULONGLONG __cdecl AddTimes(const FILETIME& ftA,const FILETIME& ftB){
LARGE_INTEGER a, b;
a.LowPart = ftA.dwLowDateTime;
a.HighPart = ftA.dwHighDateTime;
b.LowPart = ftB.dwLowDateTime;
b.HighPart = ftB.dwHighDateTime;
return a.QuadPart + b.QuadPart;
}
__declspec(dllexport) CString __cdecl GetProcess(DWORD arrayProcess[], DWORD numberprocess)
{
// Working set cua cac process
DWORD arrayWorkingSetSize[100];
unsigned int maxindex;
DWORD processMaxSize;
CString nameProcessMaxSize;
unsigned int i;
for( i =0; i < numberprocess;i++){
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, arrayProcess[i] ); // handle toi 1 process
if (hProcess != NULL)
{
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ){ // laays kich thuoc bo nho ma tien trinh do su dung
arrayWorkingSetSize[i] = pmc.WorkingSetSize ;
}
}
CloseHandle( hProcess );
}
// Sap xep, lay ra phan tu co WorkingSetSize lon nhat
maxindex = 2;
processMaxSize = arrayWorkingSetSize[2];
for(i =2; i<(numberprocess - 10); i++){
if(arrayWorkingSetSize[i] != 3435973836){
if(processMaxSize < arrayWorkingSetSize[i] ){
processMaxSize = arrayWorkingSetSize[i];
maxindex = i;
}
}
}
// lay ten process co kich thuoc max
nameProcessMaxSize = GetNameProcessMU(arrayProcess[maxindex]);
return nameProcessMaxSize;
}
__declspec(dllexport) CString __cdecl GetNameProcessMU(DWORD processID)
{
CString nameProcess;
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
nameProcess = (LPCTSTR)szProcessName; // chuyen kieu
CloseHandle( hProcess );
return nameProcess;
}
#ifdef __cplusplus
}
#endif
现在,当我构建它时,它一次又一次地显示错误,以发现它不知道某些功能。
Error 1 error C3861: 'GetProcess': identifier not found c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 104 1 Bkav_btap2_dll
Error 2 error C3861: 'AddTimes': identifier not found c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 154 1 Bkav_btap2_dll
Error 3 error C3861: 'AddTimes': identifier not found c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 155 1 Bkav_btap2_dll
Error 4 error C3861: 'GetNameProcessMU': identifier not found c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 187 1 Bkav_btap2_dll
Warning 5 warning C4190: 'GetProcess' has C-linkage specified, but returns UDT 'ATL::CStringT<BaseType,StringTraits>' which is incompatible with C c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 200 1 Bkav_btap2_dll
Error 6 error C3861: 'GetNameProcessMU': identifier not found c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 234 1 Bkav_btap2_dll
Warning 7 warning C4190: 'GetNameProcessMU' has C-linkage specified, but returns UDT 'ATL::CStringT<BaseType,StringTraits>' which is incompatible with C c:\users\anhnt\documents\visual studio 2010\projects\bkav_btap2_dll\bkav_btap2_dll\bkav_btap2_dll.cpp 239 1 Bkav_btap2_dll
请帮帮我:(。谢谢
P/s:我读了这个主题(stackoverflow.com/questions/17332327/cannot-build-dll-with-base-class),但它对我不起作用。
【问题讨论】:
-
您在使用函数时没有在文件顶部正确声明它们。
-
我在顶部声明了它们,但它仍然不起作用
-
您似乎也忽略了错误检查。你有时会检查错误,但不管怎样就继续努力。
-
不,我不是。只是我尝试了一些方法,但它不起作用。
标签: c++ visual-studio-2010 dll mfc