The system throws exception
System.NullReferenceException: 'Object reference not set to an instance of an object.'
If anyone knows solution kindly help
What I have tried:
I am trying to use a c++ function that return LPTStr into c#
This is my dll.cpp file
;extern "C" __declspec(dllexport) LPTSTR PID_GetProcessName(int a) {
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded);
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return NULL;
}
cProcesses = cbNeeded / sizeof(DWORD);
TCHAR szProcessName[MAX_PATH];
for (int i = 0; i <= cProcesses; i++)
if (i == a) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, aProcesses[i]);
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
LPTSTR b = szProcessName;
return b;
}
}
CloseHandle(hProcess);
break;
}
}
This is my c# code
[DllImport("D:\\RunningProcessDll\\Debug\\RunningProcessDll.dll",
CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.LPTStr)]
public static extern string PID_GetProcessName(int a);