Home > Software engineering >  C # call C DLL strange questions
C # call C DLL strange questions

Time:10-05

Background: c # web project called c + + DLL, the DLL encapsulates the network library, useful MFC network database, the function of vc, the other business threads in the DLL using c + + standard library functions,
Question:
1. C # call c + + interface function, the parameters for the convenience of the c + + callback callback functions, such as void (F) F this form, now call c + + c # interface is no problem, it was decided that the problem is that if I'm in c + + thread calls the registered callback function with the c + +, c #, if a thread is created using AfxBeginThread function thread c + + program will collapse, I also collapsed, if is to use the standard library STD: : thread created thread wouldn't, solving reason,
2. When close the c # console application, c + + destructors used inside lock function related to the program will crash, prompt netdll. DLL errors, tried a lot of methods are not line, now a temporary solution is in c + + destructors to stop business thread (thread of business is to use STD: : thread created, network thread is to use AfxBeginThread) program after sleep for 5 seconds (3 seconds), the great god help resolve oh,

CodePudding user response:

Generally does not directly open a thread in the DLL, only do calls a method, immediately return a result,
Really no exe in open threads, call the DLL method,

1 May is MFC something not initialize
2 way to increase a DLL, stop the DLL which of all, stop and quit

CodePudding user response:

C # delegate, calling convention is that clear

CodePudding user response:

The coupling between managed and unmanaged so tight

CodePudding user response:

Afxbeginthread I remember his thread function is UINT method (LPARAM) form

CodePudding user response:

What is the main MFC with c # some conflict

CodePudding user response:

refer to the second floor Dobzhansky response:
c # delegate, calling convention rules clearly?

Calling convention cdcal, stdcal within all in tune

CodePudding user response:

reference 5 floor oyljerry reply:
mainly MFC with c # some conflict what

Form is the program crashes, submitted to the c + + DLL errors

CodePudding user response:

Dobzhansky
reference 3 floor response:
coupling between managed and unmanaged so tight it

DLL is encapsulated inside a doll before writing the iocp network library, c + + used for binary data processing, results to back off the way back to the c # function

CodePudding user response:

I feel now that he USES afxbeginthread, estimation is a MFC extension DLL form, if is to extend the MFC DLL form, it seems impossible to perfect call c #,
Solution, write a pure c exe, calling the DLL, see not wrong? If not, change the c exe to DLL, encapsulation, again

CodePudding user response:

I suspect some of problems in the second phenomenon now, garbage collection is c #, is closing the c + + immediately when washing the dog function called to stop a thread, such as c # can't find the c + + to create threads when garbage collection system automatically associated with c # something (what), so netdll. DLL errors, sleep 5 seconds in the c + +, c #, such as garbage collection to complete then call c + + it is ok to wash a dog, according to this idea, I join in the c # main function closes the window events
 GC. Collect (); 
GC. WaitForPendingFinalizers ();
GC. Collect ();

Still no tragedy

CodePudding user response:

HDT, 9/f,
reference response:
I felt now that he USES afxbeginthread, estimation is a MFC extension DLL form, if is to extend the MFC DLL form, it seems impossible to perfect call c #,
Solution, write a pure c exe, calling the DLL, see not wrong? If not, change the c exe to DLL, encapsulation, again

In c + + exe calls the DLL is not wrong, I'm more strange for what I am in business in the thread in c + + standard library functions STD: : thread won't go wrong

CodePudding user response:

You take a look at this

CodePudding user response:

Don't use the c + +/CLI, MFC indirectly with the CLR interactions, whether to need to set up is not clear, but you can have a try,
In the link set, advanced, the CLR Thread Attribute selection STA

CodePudding user response:

This kind of interaction, using MFC less, had better not,

CodePudding user response:

Don't be A language code is modified to B language code busywork,
Also don't use A language code to directly invoke B language code base, this complicated things so easy to get wrong,
Just make A, B language code of input and output is redirected to A text file, or modify A, B language code let it through text file input and output,
Can easily make A, B coordination between the two languages,
For example:
A will request data written to A file a.t xt, renamed after finish aa. TXT
B find aa. TXT, read its contents, call the corresponding function, and writes the results file b.t xt, after finish delete aa. TXT, changed its name to bb. TXT
Found A bb. TXT, read the content, after finish delete bb. TXT
Above can be replaced by any kind of A language or development environment, B can be replaced by any kind of with the development of A different language or development environment,
Unless A or B does not support to determine whether A file exists, file read and write and file name,
But who can name does not support to determine whether a file exists, file read and write and file name for the development of language or development environment?
Can put the temporary files on the RamDisk efficiency decrease wear disk,
Data structure is very complex, a text file format problems refer to a json or XML

The communication methods between the temporary text file sharing this process there are plenty of advantages, compared to other method only listed below I can think of now:
Loose coupling between process,
Can be on the same machine, process, also can cross machine, across the operating system, hardware platform, and even multinational,
, convenient debugging, and monitoring, only let the third party or artificial view the temporary text files,
Switch, convenient online service, need to delete or create the temporary text files,
, is convenient to realize distributed and load balancing,
Services to provide convenient, queue, queue is full and it is almost impossible to happen (unless the hard disk space full)
DE...

"Across different languages, machine, across the operating system, hardware platform, multinational, cross *. *" misery,
Back is "the use of Shared a plain text file information communication" of the shore!
  • Related