开发者

WriteFile() crashes when trying to carry on multithreading

开发者 https://www.devze.com 2023-04-06 19:15 出处:网络
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 por

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 proble开发者_JAVA百科m 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);


This is wrong: sizeof(TCHAR). Since you are using char you should use sizeof(char) instead. TCHAR could be either 1 or 2 bytes...

In the call to WriteFile you should use Length instead of sizeof(tBuffer). Otherwise you'd probably end up with garbage data in your file (which I assume is later read from somewhere else).


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消