I am working on an mfc dll that is accessed via a script and all this works fine. I have added a multi-threading component to it and am trying to use the WriteFile() function to write to my serial port, but somehow the WriteFile() function exits the application after the 2nd write command gets executed.
Without the multithreading bit, everything works normally and I can apply as many writefile commands as I want.
Multi-threading: I am using
CreateThread(NULL,0,WorkerThread,this,0,0);
to create my thread. Using "WorkerThread" to carry out the writefile operations described earlier in the background.
Additionally, I need to use the Sleep() function while writing it at intervals defined by me. At the moment, the program just quits when trying to use Sleep(). So, I just removed it for the time being but would need it at a later stage.
Is this a known problem or something with a but-obvious solution?
Update: I have sort of tried to reach somewhere close to the problem but still not been able to resolve it. Apparently it looks like there is some problem with my WriteFile() parameters.
WriteFile(theApp.m_hCom,tBuffer,sizeof(tBuffer),&iBytesWritten,NULL);
It is not taking the sizeof(tBuffer) properly and because of which it is crashing. I checked out the string to be passed, which is exactly equal to what I need to pass but its crashing out the program if I write the code as done above (for WriteFile()). When I keep the stringlength i.e. manually set the sizeof(tBuffer) parameter to 14, then the program runs but the command does not get executed as the total string size of buffer is 38.
CString sStore = "$ABCDEF,00000020,01000000C1200000*##\r\n";
char tBuffer[256];
memset(tBuffer,0,sizeof(tBuffer));
int Length = sizeof(TCHAR)* sStore.GetLength();
memcpy(&tBuffer,sStore.GetBuffer(),Length);
and then sending it with the WriteFile command.
WriteFile(theApp.m_hCom,tBuffer,sizeof(tBuffer),&iBytesWritten,NULL);
I'm guessing its crashing because you are trying to run that directly from your DLL. Write Function looks fine to me and I think if you try to run your program from the Python script ONLY, it should work. I have faced something similar earlier and came to the conclusion of not running my DLL through the debugger but just the script.
Please read this and this for more information.
Hope this helps.
Good Luck!