1

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:

2015\projects\consoleapplication1\consoleapplication1\consol‌​eapplication1.cpp(23‌​): error C2440: '=': cannot convert from 'char [100]' to 'LPWSTR' 1> c:\users\gabriel\documents\visual studio 2015\projects\consoleapplication1\consoleapplication1\consol‌​eapplication1.cpp(23‌​): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

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++ ?

acraig5075
  • 10,588
  • 3
  • 31
  • 50
Gabriel
  • 25
  • 1
  • 4
  • Lots of people are using out-of-date Visual C++ compilers, but the one you have bundled in VS 2015 is quite recent (and you can get an even newer one for free, the release candidate of Visual Studio 2017 is public now) – Ben Voigt Nov 22 '16 at 21:13
  • `__imp_GetOpenFileNameA` -- That's a linker error, not a compiler eror. You failed to add the requisite Win32 import libraries to your project. – PaulMcKenzie Nov 22 '16 at 21:17
  • @PaulMcKenzie: I think Gabriel knows that, he said "it doesn't compile in VS" (probably precompiled header configuration issues) then "but it does in Dev C++". So he knows he's getting past the compilation step in Dev C++. – Ben Voigt Nov 22 '16 at 21:19
  • *I get a load of errors.* -- What are the errors? Are you compiling a Unicode or MBCS based application? If it's Unicode, your character types you're using for functions such as `MessageBox` are incorrect (are those your errors?). Visual Studio projects default to Unicode, and your code sample assumes MBCS. – PaulMcKenzie Nov 22 '16 at 21:30
  • I don't even know what you are talking about ! Care to explain it to me ? Thank you. – Gabriel Nov 22 '16 at 21:38
  • @Gabriel Please post the errors you say you're receiving. The string types you use in a Windows applications depends on the build type. In the configuration for your project, is it using Unicode or MBCS (or None)? If it's Unicode, you should be getting a ton of errors since all of your character types you're using now are non-wide (ANSI) characters, not wide characters. – PaulMcKenzie Nov 22 '16 at 21:41
  • 1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------ 1> ConsoleApplication1.cpp 1>c:\users\gabriel\documents\visual studio 2015\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(23): error C2440: '=': cannot convert from 'char [100]' to 'LPWSTR' 1> c:\users\gabriel\documents\visual studio 2015\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(23): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast – Gabriel Nov 22 '16 at 21:46
  • @Gabriel -- Take a look at the [OPENFILENAME struct](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646839(v=vs.85).aspx). Do you see that the types are `LPTSTR`, `LPCTSTR`, etc. Those are **not** `char` -- those are based on `TCHAR` which is different if the build type is Unicode. – PaulMcKenzie Nov 22 '16 at 21:46
  • Well, there's your problem, exactly as I predicted. You are using a Unicode build, and giving ANSI strings and string pointers to functions that take wide strings. – PaulMcKenzie Nov 22 '16 at 21:49
  • @Gabriel Also, [here is a sample that compiles](http://rextester.com/ZOKS4404). Ignore the linker errors, the point is that the code now compiles using Visual Studio. Note the changes made. – PaulMcKenzie Nov 22 '16 at 22:01
  • So my Dev C++ uses MBCS and VS is using UNICODE and that is why some programs compiles in one and not the other ? – Gabriel Nov 22 '16 at 22:56
  • @Gabriel In a nutshell, yes. You can change the VS character type by going into the Project Configuration and changing the character type to ANSI (or None). But I don't recommend this, as there are very few Windows apps these days that are ANSI. So what you really should be doing is getting your Dev C++ (actually the compiler is g++) to handle Unicode, not change VS to use ANSI. In other words, get the same compiler errors using Dev-C++ that you were getting with Visual Studio. Then you know you've got your app changed to Unicode. – PaulMcKenzie Nov 22 '16 at 23:03
  • I notice it says console application which is in a command prompt Window, did you try a dialog based test application? – Andrew Truckle Nov 29 '16 at 03:26

1 Answers1

0

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720