CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C,C++ Linking

  1. #1
    Join Date
    Apr 1999
    Posts
    6

    C,C++ Linking

    Now I am programming C program.(About 15,000 line)
    But this program consists of only WIN32 APIs.
    I want to link my program with VC++ Class.
    Because my C program has some bugs in File Open Dialogbox,
    I want to use CFileDialog Class.
    Is it possible?
    Please help me.

    [email protected]


  2. #2
    Join Date
    May 1999
    Posts
    69

    Re: C,C++ Linking

    It is certainly possible. One of the first things to do is to check through a typical MFC program and notice the headers and libraries that are included in your makefile.

    If your program is a console like C program it is probably easiest to create yourself an MFC console application and then just paste your code in.

    If your program is a GUI application, you will already have a message loop and you'll need to replace this (and probably lots more) to get a build.

    If the worst comes to the worst, you should be able to go into the MFC source code and pull out the class declaration and definition of CFileDialog (and related code) and insert it into your C program without too much hassle.


  3. #3
    Join Date
    Apr 1999
    Location
    Chennai, India
    Posts
    48

    Re: C,C++ Linking

    hi,

    Have an extern function in the C file.
    Define the function in the CPP file. This function has to be have the linkage specifier extern "C". dont forget to do this !

    A small code have to be written before doing anything with MFC resource.
    Call AfxSetResourceHandle ( ) passing it the module handle that has the resource.
    If u dont have a seperate DLL give the instance handle that u get in WinMain.
    Then instantiate an object of type CFileDialog and use it as u wish !

    if u dont call AfxSetResourceHandle u get an assertion failure !

    The code will be -

    In ur C file have
    extern void Somefuncion ( /* your parameters */ );
    HINSTANCE ghInstance;

    .
    .
    .
    ghInstance = hInstance; /* instance handle of the module containing the resource */
    Somefunction ( /* params if u have any */ );
    .
    .
    .

    In Another CPP file

    extern "C" HINSTANCE ghInstance;
    extern "C" void Somefunction ( /* params if u have any */ )
    {
    AfxSetResourceHandle ( ghInstance );

    /* do anything here to process the args */
    CFileDialog fd(TRUE);
    fd.DoModal();

    /* fd.GetPathName() gives the filename */
    /* anything u wish here */
    }

    bye.


  4. #4
    Join Date
    Apr 1999
    Location
    Chennai, INDIA
    Posts
    27

    Re: C,C++ Linking

    hi,
    In MFC when you give File Open Command, it calls the CWinApp's member function DoPromptFileName. this in turn calls CDocManager's DoPromptFileName. the actual code for displaying the open dialog box is present in CDocManager:oPromptFileName (DOCMGR.CPP). copy that code and make the necessary changes and you'll get the answer.

    Regards,
    Kalyan

    P.S:
    you can even customize the Open Dialog box as you wish.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured