Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Question regarding proper usage of CoInitialize/CoUninitialize for program that calls C# DLL

$
0
0

Under VS2005, I developed a unmanaged C++ EXE (that also uses MFC in a Shared DLL) that calls our C# DLL assembly.

 

Both the C# DLL and C++ EXE were built based on the information provided at this link.

http://support.microsoft.com/kb/828736/en-us

The program was converted to VS2010 a few years ago with no modifications that I can recall.

The settings for the C++ EXE are:

General->Common Language Runtime Support = Common Language Runtime Support (/clr)

 

Linker->Advanced->CLR Thread Attribute = STA threading attribute (/CLRTHREADATTRIBUTE:STA)

 

Linker->Advanced->CLR Image Type = Default Image type

 

The "main" EXE's cpp file is coded like this:

 

//  In "include#" area:

#import "ManagedLibrary.tlb" raw_interfaces_only

using namespace ManagedLibrary;

 

int main(void)

{

process_a_file();

}

 

//  Process a file

void process_a_file(void)

{

// Read file - call C++ function that calls the C# DLL

while ! EOF

{

call_managed_DLL();

}

}

 

void call_manged_DLL(void)

{

    CoInitialize (NULL);

 

    //  Call the exposed C# method of the DLL

          CallSomeCSharpMethod();

 

    CoUninitialize();

}

 

Is this the correct usage of the CoInitialize/CoUninitialize? I thought I had come across something that said "CoInitialize" should only be done once (like in main) and 'CoUninitialize" in main before the program exits.

 

Has anything changed under VS2010 (and newer VS versions) that the program should be "coded differently"? I've just started reading books on C++/CLI…

 

Thanks.


Viewing all articles
Browse latest Browse all 15302

Trending Articles