Using GetOpenFileName in Dev C++, VS

旧街凉风 提交于 2021-02-11 05:12:23

问题


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


回答1:


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

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.



来源:https://stackoverflow.com/questions/40751685/using-getopenfilename-in-dev-c-vs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!