I’m trying to pass a handle from process1 to process2 using the DuplicateHandle function. I obtain the handle using the CreateFile function:
HANDLE COMportHandle;
COMportHandle = CreateFile(TEXT("COM5"),
GENERIC_ALL | PROCESS_DUP_HANDLE,
0,
0,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
0);
In the second process, I first obtain the process ID from process1 through shared memory, and then try to duplicate the handle:
HANDLE pr1handle, CPH, COMportHandle;
pr1handle = OpenProcess(PROCESS_DUP_HANDLE,FALSE,process_id);
if(!DuplicateHandle(pr1handle,COMportHandle,GetCurrentProcess(),&CPH,PROCESS_DUP_HANDLE,FALSE,0))
printf("Error: %d\n",GetLastError());
Then I get the ERROR_INVALID_HANDLE.
The processes are not related, I run the first to open the COM port, and then want to be able to read from it with the second process.
Can somebody tell me where the catch is?
In this code here:
you introduce a new, uninitialized COMportHandle. So, assuming this is the actual code, I’m not at all surprised COMportHandle is invalid”. You will somehow need to get the actual value of COMportHandle from your first process.