Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Problem with form project

Hi,
Further to this thread
https://www.experts-exchange.com/questions/28629782/Details-to-do-the-search.html?anchorAnswerId=40677458#a40677458

I want to retrieve the details of one of the files, like
https://dl.dropboxusercontent.com/u/40211031/flname_1.idx

on the form. As C++ is not supposed to handle Windows form, should I use C# instead, to do this? Is there any similar example, that is doing the similar thing?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

>>As C++ is not supposed to handle Windows form

C++.net does work with Windows Forms.  (Not quite the same as classical, unmanaged, C++ which I guess is what you are thinking of).
Avatar of Peter Chan

ASKER

Yes, is there any example of C# codes, doing the similar like what I mentioned? Thanks.
Any other help?
I don't actually know what you want to do, it isn't clear here or in the link you posted.
If you have a C++ example why isn't that good enough for you?  You could try to use the code in a win form app with C++.net OR put the C++ code into a dll which you call from a C# win form app if you would prefer.
I mean one other example/notes to retrieve records from the relevant binary file.
Did you open and see the binary file, I mentioned in above? Thanks
I don't actually know what you want to do, it isn't clear here or in the link you posted.

>>Did you open and see the binary file, I mentioned in above? Thanks
No, what application is it supposed to be opened with ?


ps.  If you have an example I really don't see what your problem is.  You can either use the code directly in your app or indirectly by wrapping it in a dll that your app calls.
I need the way to retrieve records from the given binary file. This is what I expect to have.
the file flnames_1.idx contains records of the following structure:

struct index_name
{
    char name[21];
    unsigned int recnum;
};

Open in new window


depending on whether the structure is packed or not, one record has a size of 25 or 28 bytes.

you can access the file by code like

bool getrecord(std::ifstream & file, unsigned int rec, index_name & index)
{
       long long pos = rec;
       pos *= sizeof(index_name);
       if (!file.seekg(pos))
       {
             std::cout << "error seekg at " << pos << "  last error " << errno << std::endl;
             return false;
       }
       if (!file.read((char*)&index, sizeof(index_name))
       {
             std::cout << "error read at " << pos << "  last error " << errno << std::endl;
             return false;
       }
       return true;
}

the records of the index files are sorted by name. because of that it is also possible to have an access by name by means of a binary search (there is already a program readbinaryfile which worked).

As C++ is not supposed to handle Windows form, should I use C# instead
beside of windows form, it is also possible to use an mfc application which might be easier.

if that is an option you may create a new project of type mfc application and make it of type dialog-based. then you could edit the form created by the wizard to contain a prompt + edit field for the file (path), and a prompt + edit field for the record number to search for. finally add two read-only edit fields for the index members and a button 'retrieve record'.

add a handler for button click, read path and record number from screen, then open the file and call above function to retrieve the record. if found you put the members to output fields.

Sara
Many thanks Sara.
I want to read/retrieve all details from binary file and then display them record by record on the Windows form. How to call one mfc Application, from one C# Form project?
How to call one mfc Application, from one C# Form project?
that doesn't make sense. mfc (Microsoft foundation classes) is an alternative to windows form in c++. it doesn't need .net what is an advantage if you don't have much experience with gui programming. it is an disadvantage if you already made form programs with c#  or vb. in either case it is the fastest way for the task to read from binary files and show the records, since you can use many of the current structures and code directly without porting it to a new language.

Sara
Your second comment in this question implied you have an example of doing what you want in C++.

Do you have a C++ example that does what you want?

If yes then what exactly is your problem?
Many thanks Sara.
that doesn't make sense. mfc (Microsoft foundation classes) is an alternative to windows form in c++

Is it to call "ReadBinaryFile" within one .mfc project? Any example/demonstration to that?
- create a new project of type mfc application
   - make it of type dialog-based.
   - the wizard would show an empty form which you can edit
   - edit the form to contain
      - a prompt + edit field for the file (path),
      - a prompt + edit field for the record number to search for.
      - two read-only edit fields for the index members 'Name' and 'RecNum'
      - a button 'Retrieve Record'.
   - right-click at the edit field for file path and add a CString member variable by wizard
   - right-click at the edit field for record number and add a int member variable by wizard
   - right-click at the read-only edit field for name and add a CString member variable
   - right-click at the read-only edit field for recnum and add a int member variable
   - right-click at the button and add a handler for button click,
       - implement the handler of button-click
            - call UpdateData(TRUE); // gets data from screen to members
            - open the file and call function to retrieve the record (see code above)
            - if record found assign record members to corresponding dialog members
                - call UpdateData(FALSE);  // puts data from dialog to screen (shows record data)

Sara
Many thanks. Which is the form below?
User generated image
you created an mdi mfc project (multi document interface). you should have created a dialog-based mfc project.

for an mdi project you have a view class CMfcAppView which currently is derived from CView. for your purposes you have to derive it from CFormView which would use a dialog resource same as a dialog-based mfc application. so you could add a dialog form to your project by using the resource editor. then change class CMfcAppView that it is derived from CFormView and not from CView (changes have to be made both in mfcappview.h and mfcappview.cpp where CView was exchanged by CFormView for any occasion). then you need to change the constructor of the CMfcAppView that it passes the IDD resource id to the CFormView.

an easier way is to create a further mfc application named ShowBinaryFile and make it dialog-based (what is one of the first decisions you have to take when using the wizard to generate the project).

Sara
Many thanks. Is ChildFrm CFormView or not? If not, which is CFormView below?
User generated image
your view class is CMfcAppView.

your frame class (that is the outer frame with menu and toolbar buttons) is CMainFrame as long as there is no document opened. it changes to CChildFrm after a document was loaded (opened) or a new document was created and a view was shown where you can edit the document. note, as long as the view class is not derived from CFormView you will not be able to bind the view to a form. the view would only show an empty window.

generally, an MDI application has an application class derived from CWinApp, a main frame class (linked to resource id IDR_MAINFRAME), and for each document type supported at least one triple of a document class derived from CDocument, a frame class derived from CMDIChildWnd, and a view class derived from CView (or CScrollView or CFormView, more ...). you could add more views by defining another triple for the same document class and the same frame class. this normally was done in the application class (mfcapp.cpp) in the Init_Instance member function. if you look into the code you will see a CMDIDocTemplate class used which is a helper for to creating such a triple of document, frame and view.

note, an MDI application is a framework for a rather big application with menus, toolbars, many docked windows, different models. visual studio is a sample of an mdi application. if you only need a form where you can input a path and a record number to lookup for a record in a binary file, an MDI application is not only an overkill, but simply not suitable for the task.

Sara
Many many thanks Sara.
What should I choose below to the options of the project?
User generated image
use the 3. option 'dialog based'.

that creates an application containing one form controlled by one dialog class. the class object was created in CMfcApp::InitInstance and invoked by DoModal call from there.

Sara
Many thanks Sara. Do I need to further choose the options below?
User generated image
And also need to select anything below?
User generated image
you may use the defaults because it doesn't make a difference for your purposes.

the main thing of the defaults is the Unicode support and the precompiled headers via stdafx.h. I personally deselect both options but the options are not crucial and may deselected later if you don't need them.

Sara
Good day Sara,
generally, an MDI application has an application class derived from CWinApp, a main frame class (linked to resource id IDR_MAINFRAME), and for each document type supported at least one triple of a document class derived from CDocument, a frame class derived from CMDIChildWnd, and a view class derived from CView (or CScrollView or CFormView, more ...). you could add more views by defining another triple for the same document class and the same frame class. this normally was done in the application class (mfcapp.cpp) in the Init_Instance member function. if you look into the code you will see a CMDIDocTemplate class used which is a helper for to creating such a triple of document, frame and view.

Do you mean  I should start with ClassView.cpp and MFCApp.cpp? Can I have more details to the way?
Do you mean  I should start with ClassView.cpp and MFCApp.cpp?
as far as i know your requirements, an mdi app is an overkill. my recommendation is (and was) to create a new mfc project which is dialog-based. the framework consists only of two classes then: an application class derived from CWinApp and a dialog class derived from CDialog.

that allows you to quickly edit the default form generated with the dialog class to your needs.

Sara
Many thanks.
Is there one example to this?
if you choose the dialog-based mfc application, the wizard will generate a default project with all classes and a form which you easily can enhance to your requirements. by right-clicking on the controls you can add members and event handlers with a few mouse clicks.

Sara
Many thanks Sara.
I don't know why I cannot build the project attached.
https://dl.dropboxusercontent.com/u/40211031/MFCApplication1.zip
what happens when you build the project?

as far as i could see, you didn't add neither resources nor code to the generated code.

Sara
Many thanks Sara.
The following is what I got
User generated image
as far as i could find an explanation, the problem can occur if you mix-up managed and unmanaged elements in your project. if you changed some of the defaults when creating the project (for example when you added CLI or X64 to the project), you should create a new mfc project where you create just a "normal" 32-bit MFC application, dialog-based without managed code. if that doesn't help, you can try to switch off precompiled header option in the c++ properties. after you did so, the stdafx.cpp (not the stdafx.h) should be deleted from project tree.

if the problem still occurs, check your configuration. you should begin with 'Debug' but if you make some changes there (for example when removing precompiled headers) also change the 'Release'  configuration accordingly. if you currently see a configuration which is neither 'Debug' nor 'Release' but soemthing like 'Mixed Mode', then this configuration most likely is responsible for your issues. change the configuration to 'Debug' and try again.

Sara
Many thanks Sara.
I switch off precompiled header option but still get the same problem, while I see Configuration is "Debug".
this kind of error sometimes appears with visual studio (all versions since more than 20 years) and has different causes.

if it cannot be solved by changing configurations or undoing last changes you should close visual studio, reboot, and try again. if it is still there, delete the whole project induding all files and folders (you may save source files .cpp, .rc, .h, .c somewhere else) and create a new project using a different name (that is important to avoid issues due to ambiguous file names). don't do any changes to the defaults before your first successful build. for example, always use mfc from dll and from static libraries.

if nothing helped, the cause probably was because of the environment or because of some installations (3rd party, sdk, ...).  you either have to go to an older version of visual studio or reinstall or use a different system if possible.

Sara
Many thanks Sara.
I really got the same problem within both Win 2008 server and Win 2012 server. Is there any other way to achieve the original request, other than using MFC dialog based application?
you could try mfc SDI application and turn your view class to CFormView by using the wizard. then the dialog form can be used in that form view.

but i have doubts that this will help to avoid the issues you had.

Sara
Many thanks Sara.
I think I should also ask MSDN C++ forum to see if this is really one bug of VS 2013 or not.
I may close this thread soon.
compiler crashes are almost every time due to installation or environment conflicts.

so you may try to uninstall and reinstall the visual studio on the server and do the second "as administrator".

also if you run the visual studio at the server, you should have local administrator rights such that the registry entries below HKEY_LOCAL_MACHINE can be used without restrictions. finaly, the .NET installation on the server must fit to the one needed by the visual studio installation. normally, this is guaranteed by the setup but for a server installation there may be some restrictions that could prevent from a properly working visual studio.

Sara
Many thanks Sara.
If I've re-done the same on another Win 8.1 machine, on which I've re-setup VS 2013, I then got this
Error	1	error C2471: cannot update program database 'f:\mfcapplication1\mfcapplication1\debug\vc120.idb'	f:\mfcapplication1\mfcapplication1\stdafx.cpp	9	1	MFCApplication1
Warning	2	warning D9028: minimal rebuild failure, reverting to normal build	f:\MFCApplication1\MFCApplication1\cl	MFCApplication1

Open in new window

error C2471: cannot update program database 'f:\mfcapplication1\mfcapplication1\debug\vc120.idb'

this can happen if the debugger was still active or if you opened the same project twice, or if you copied whole project with debug and release folder from other machine.

you may try to delete debug and release folder, or even better, create the project again from a new project folder.

did you check whether the file f:\mfcapplication1\mfcapplication1\debug\vc120.idb exists? if yes, try to delete it. if the file is locked or read-only and you can't delete it, you may reboot and/or login as administrator.

Sara
Many thanks Sara.

It is fine now to setup. I don't see ClassView.cpp below
User generated image
what should I select to "Create project wizard"?
classview.cpp was only in sdi and mdi mfc application. it is a 'sample' view to be 'docked' into the main frame to visualize some arbitrary data in a tree.

your current choice is one dialog form coded in MfcApplication4Dlg and started from MfcApplication4 as a modal dialog.

you may now design the dialog form and post. then we could design the access to the files and the way hot to present the results.

Sara
classview.cpp was only in sdi and mdi mfc application. it is a 'sample' view to be 'docked' into the main frame to visualize some arbitrary data in a tree.

Many thanks Sara. Is there any sample project to this?
actually, the default project generated by wizard for mdi and sdi, already contains a 'sample' view. that is what you started with. in the left-bottom docking area was a 'class view' to 'fake' classes. the 'view' was created in mainfrm.cpp and implemented (only dummy contents) in classview.cpp.

i told you that such a 'huge' application with so many classes and framework stuff is much more than you would be able to overlook and have a chance to adopt to your needs.

perhaps you should describe what your application should do. try to design one or more forms and think about user input and possible visualisation. if you would post these considerations we surely can give you valid advice which way to go and how to proceed.
Many thanks Sara.
I do not see

mainfrm.cpp

to the project shown below.
User generated imagewhat to adjust to the project, as I did choose "Dialog" to the project?
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks Sara.
I can see MainFrm.cpp now. Is there any sample to refer to the previous structure we have, using MFC application?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks Sara. Is it a problem to 1st line below?
User generated image
yes. should be #ifndef and be closed by a corresponding #endif

this kind of macro is called header protection. it shall prevent the header to be included a second time, what would happen for example if b.cpp includes b.h and a.h and b.h also includes a.h. by using a header protection the second inclusion would find out the a.h already was included and do nothing.

Sara
the only include it needs is binaryfile.h. then copy the functions from readbinaryfile.cpp to the new file.

Many thanks Sara.
Is it to copy all codes from readbinaryfile.cpp into the current project, other than your advice to .h file?
Is it to copy all codes from readbinaryfile.cpp into the current project, other than your advice to .h file?
no. because you may need to include the h file from more than one cpp file in the mfc project. if the h file contains implementations of functions, multiple inclusions from different cpp file would lead to an error since the linker would complain about duplicate 'symbols'. you could try to make the functions 'inline' or put a class around them, what also would help, but in my opinion it is not worth the efforts.

so the easiest is to use a new cpp file binaryfile.cpp and put the functions like 'createItemText' into it. the binaryfile.h only contains a "declaration" (prototype) of the function what simply is the return type, the function name and the arguments closed by a semicolon ;.  note, when adding the binaryfile.cpp to your project, you should switch off 'precompiled header' for this cpp file (right-click in the solution tree, properties, select 'all configurations', goto 'configuration properties - c/c++ - precompiled headers - 'not using precompiled headers'). alternatively add #include "stdafx.h" as first statement.

note, if you do so, you could use the two binaryfile sources for readbinaryfile and savebinarfile as well (though i wouldn't change working projects only because of this).

Sara
Many thanks Sara.
How does BinaryFile.cpp interact with MFCApplication2.cpp, while the latter is the one being created, when creating the project?
What is the purpose of BinaryFile.cpp? Is there any example to what you've suggested?
How does BinaryFile.cpp interact with MFCApplication2.cpp?
i don't know. you never told me what you intend to do with the form project.

What is the purpose of BinaryFile.cpp?
it provides the structures and access functions to the files created by savebibaryfile and queried by readbinaryfile.

Sara
Thanks Sara.

How about that I want to show the details of the records we did create most recently? Is there any example to do this?
Good day Sara,
Can you please see this only if available? thanks a lot
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks Sara.
Sorry for that I got problem to randindex below

Error      2      error C3861: 'randindex': identifier not found      c:\mfcapplication2\mfcapplication2\binaryfile.cpp      41      1      MFCApplication2
the binaryfile.h only contains a "declaration" (prototype) of the function what simply is the return type, the function name and the arguments closed by a semicolon

Exactly, what are the details I should put into binaryfile.cpp?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many many thanks Sara.
I can compile the codes now.

you may add radio buttons for
 
index number
 
index name
 
record structure

by those the User may chose which kind of binary data they would like to look for. name the id's of the 3 radio buttons IDC_RAD_NUMBER, IDC_RAD_NAME, IDC_RAD_RECORD. the idc numbers will be stored in resource.h. make sure that the 3 numbers are contiguous (what is automatical the case if you define them straight one after the other.


Should this to use form design, and if yes, is there any example to that?
the wizard already created an empty form for you. you may open it by choosing tab for resource view rather than solution explorer for the left tree in visual studio. then look in dialog resources where is one for your dialog. in 'view menu' you can choose 'tool box' where all available controls can be chosen and placed into the dialog form.

Sara
is there any example to that?
it is rather intuitive to use. if you have chosen a control on your form you may type ALT+ENTER to get the properties window for the control. if you got problems using it you may look for a mfc tutorial or youtube sample.

Sara
the wizard already created an empty form for you. you may open it by choosing tab for resource view rather than solution explorer for the left tree in visual studio.
Many thanks Sara.

Sorry for that I do not see one empty form, within the project.
User generated image
as told, you see the dialog resources in the "resource view", which is available by a tab at the bottom of "Solution Explorer" (what is the tree in the image you posted).

alternatively, you could open the 'Resource Files' in the tree of solution explorer and double-click on .rc file (what should be named MfcApplication2.rc in your case).

Sara
I can see Class View but no resource view below.
User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial