views:

202

answers:

2

I have an existing project using c/c++ .NET. Currently I have been given a task to create a windows form from my existing code. So i have add new project windows form application in the existing c/c++ projects.form.h, form.cpp has been automatically created. Now I am having problem to call the window from my c files. Even i could not call the form.h file from my c program. Is there any solution for this problem. Listed here is the coding....

login.c

int LoginMain(int id,int task)  
{   
LoginClear();  
LoginEntry(id,task);    
dp_in = 1;  
Rep(); 

//I WOULD LIKE TO CALL THE FORM AT THIS STAGE


Cashier();   
dp_in = 0;  
Login();   
return(0);          
}

form.cpp

[STAThreadAttribute]  
int main(array<System::String ^> ^args)  
{
// Enabling Windows XP visual effects before any controls are created  
Application::EnableVisualStyles();    
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it   
Application::Run(gcnew Form1());    
return 0;    
}
A: 

You'll have use COM to expose .NET code to C++ code:

1. Mark the interface you want to expose with COM attributes:

[ComVisible(true)]
[Guid("5CBA864C-C063-4a47-2344-56AE016ABEE3")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IExposedInterface
{
    void ShowForm();
}

2. Mark the implementation you want to expose with COM attributes:

[ComVisible(true)]
[Guid("BE337127-0DF7-2344-AD66-2338FE3926D8")]
[ProgId("NETAssembly.ExposedInterface")]
public sealed class ExposedInterface : IExposedInterface
{
}

3. Use regasm.exe to generate tlb file: regasm.exe /tlb:NETAssembly.tlb NETAssembly.dll

4. In C code:

#include "stdafx.h"
#include <atlimage.h>

#import "NETAssembly.tlb" named_guids

int _tmain(int argc, _TCHAR* argv[])
{
    // Initialize COM.
    CoInitialize(NULL);

    NETAssembly::IExposedInterfacePtr pIExposedInterface;
    HRESULT hr = pIExposedInterface.CreateInstance(NETAssembly::CLSID_IExposedInterface);

    pIExposedInterface->ShowForm()

    // Release object.
    pIExposedInterface = NULL;

    // Uninitialize COM.
    CoUninitialize();

    return 0;
}
Pawel Lesnikowski
A: 

thanks for the help. I have found out that my existing program is not using /CLR but the windows form which has been created is using /CLR. According to msdn:- Caution Do not mix static and dynamic versions of the run-time libraries. Having more than one copy of the run-time libraries in a process can cause problems, because static data in one copy is not shared with the other copy. The linker prevents you from linking with both static and dynamic versions within one .exe file, but you can still end up with two (or more) copies of the run-time libraries. For example, a dynamic-link library linked with the static (non-DLL) versions of the run-time libraries can cause problems when used with an .exe file that was linked with the dynamic (DLL) version of the run-time libraries. (You should also avoid mixing the debug and non-debug versions of the libraries in one process.)

Jenuel