Gabriel Gabriel - 13 days ago 6
C++ Question

Using GetOpenFileName in Dev C++, VS

I am fairly new to programming and compilers. I had a 1 year C++ class in university. Last month I started a project where I wanted to automate some processes for work. The program takes a phone directory and then creates .cfg files from it. Nothing very advanced, mostly reading, storing and writing data.

I made my project using Dev C++ IDE. It works fine and it is simple to use for a beginner like me. Lately I was trying to use a Visual C++ function called GetOpenFileName. I got an example from the internet but it doesn't compile in Dev C++ .... Here is the example :



#include "stdafx.h"
#include <windows.h>
#include <Commdlg.h>
//
// Gobal Variables and declarations.
//
OPENFILENAME ofn ;
// a another memory buffer to contain the file name
char szFile[100] ;
int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow )
{

// open a file name
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFile ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
GetOpenFileName( &ofn );

// Now simpley display the file name
MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK);
return 0;
}





I understand this is VC++ and I am coding in C++, my question is : is it possible to make it work in Dev C++ and how ?

I get the following error : C:\Users\Gabriel\Documents\Programs\Test\main.o main.cpp:(.text+0xb4): undefined reference to `__imp_GetOpenFileNameA'

I tried to install another IDE (Microsoft Visual Studio 2015 with the C++ package). In VS, my code won't compile at all (but it compiles fine in Dev C++). I get a load of errors.

There is something I don't get... Why my code doesn't compile in VS but it does in Dev C++ ? I heard that the VS compiler is "out of date". Any way to update it ?

Or is there any way to use VC++ in Dev C++ ?

Thanks for the replies !

Gabriel

Answer

The function you're trying to call has a documentation page on MSDN, and in that page it tells you the build requirements:

enter image description here

The comdlg32.lib import library provides the missing __imp_GetOpenFileNameA symbol that the linker is looking for. Add it to your link settings (it's part of the Windows SDK, which will have been installed by Visual Studio and you might have another copy from Dev C++)

I could tell you where to find link settings in Visual C++, but although I know Dev C++ has them also, I don't use it myself and have no idea where.

If Dev C++'s compiler (gcc-based, right?) doesn't know what to do with an import library, pass its linker the name of the DLL (comdlg32.dll) instead.

Comments