在 Dev C++、VS 中使用 GetOpenFileName [英] Using GetOpenFileName in Dev C++, VS

查看:27
本文介绍了在 Dev C++、VS 中使用 GetOpenFileName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程和编译器还很陌生.我在大学上过 1 年的 C++ 课程.上个月我开始了一个项目,我想自动化一些工作流程.该程序获取电话目录,然后从中创建 .cfg 文件.没什么高级的,主要是读取、存储和写入数据.

我使用 Dev C++ IDE 制作了我的项目.它工作正常,对于像我这样的初学者来说很容易使用.最近,我尝试使用名为 GetOpenFileName 的 Visual C++ 函数.我从互联网上得到了一个例子,但它不能在 Dev C++ 中编译......这是例子:

#include "stdafx.h"#include #include ////全局变量和声明.//OPENFILENAME 的 n ;//另一个包含文件名的内存缓冲区字符 szFile[100] ;int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow ){//打开一个文件名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 );//现在简单地显示文件名MessageBox ( NULL , ofn.lpstrFile , "文件名", MB_OK);返回0;}

我知道这是 VC++,我正在用 C++ 编码,我的问题是:是否有可能使它在 Dev C++ 中工作以及如何工作?

我收到以下错误:

<块引用>

C:\Users\Gabriel\Documents\Programs\Test\main.o main.cpp:(.text+0xb4):对 `__imp_GetOpenFileNameA' 的未定义引用

我尝试安装另一个 IDE(带有 C++ 包的 Microsoft Visual Studio 2015).在 VS 中,我的代码根本无法编译(但它在 Dev C++ 中编译得很好).我收到了很多错误:

<块引用>

2015\projects\consoleapplication1\consoleapplication1\consol‌ eapplication1.cpp(23‌ ):错误 C2440:=":无法从字符 [100]"转换为LPWSTR" 1>c:\users\gabriel\documents\visual studio2015\projects\consoleapplication1\consoleapplication1\consol‌ eapplication1.cpp(23‌ ):注意:指向的类型是不相关的;转换需要reinterpret_cast,C 风格的强制转换或函数风格的强制转换

有些东西我不明白...为什么我的代码不能在 VS 中编译,但可以在 Dev C++ 中编译?我听说 VS 编译器过时了".有什么办法更新吗?

或者有什么方法可以在 Dev C++ 中使用 VC++ 吗?

解决方案

您尝试调用的函数具有

comdlg32.lib 导入库提供了链接器正在寻找的缺失的 __imp_GetOpenFileNameA 符号.将它添加到您的链接设置(它是 Windows SDK 的一部分,它将由 Visual Studio 安装,您可能还有一个来自 Dev C++ 的副本)

我可以告诉你在哪里可以找到 Visual C++ 中的链接设置,但是虽然我知道 Dev C++ 也有它们,但我自己不使用它,也不知道在哪里.

如果 Dev C++ 的编译器(基于 gcc,对吧?)不知道如何处理导入库,请向其链接器传递 DLL 的名称 (comdlg32.dll).

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

解决方案

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.

这篇关于在 Dev C++、VS 中使用 GetOpenFileName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆