19greg96
19greg96

Reputation: 2591

c++ how to initialize open file dialog (GetOpenFileName)

my code is as follows.

OPENFILENAMEA open;
ZeroMemory(&open, sizeof(open));

open.lStructSize = sizeof(LPOPENFILENAMEA);
open.lpstrFilter = "Képek\0*.jpg;*.jpeg;*.gif;*.png;*.bmp\0\0";
open.nFileOffset = 1;
open.lpstrFile[0] = '\0';
open.nMaxFile = 2048;
open.lpstrTitle = "Képek kiválasztása..";
open.Flags = OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST;

OPENFILENAME selected = GetOpenFileName(open);

My problem is, that I get the following error when trying to build: error: cannot convert 'OPENFILENAME {aka tagOFNA}' to 'LPOPENFILENAMEA {aka tagOFNA*}' for argument '1' to 'BOOL GetOpenFileNameA(LPOPENFILENAMEA)' when I call GetOpenFileName

If I call it with the open parameter as a ptr GetOpenFileName(&open) I get the following error: conversion from 'BOOL {aka int}' to non-scalar type 'OPENFILENAME {aka tagOFNA}' requested

Question: what do?

Upvotes: 1

Views: 13124

Answers (3)

dmitrycello
dmitrycello

Reputation: 184

The example in question is incomplete, and has errors. Must be the following:

OPENFILENAMEA open;
char buffer[2048]; // array to get the path
buffer[0] = '\0'; // set the first char to 0
// ...
open.lStructSize = sizeof(OPENFILENAMEA); // LPOPENFILENAMEA is pointer
open.lpstrFile = buffer; // must point to array
open.nMaxFile = 2048; // size of used array
// ...
BOOL selected = GetOpenFileNameA(&open));

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122383

Try this:

BOOL selected = GetOpenFileName(&open);

I'm not familiar with WinAPI, but check out the error message:

error: cannot convert 'OPENFILENAME {aka tagOFNA}' to 'LPOPENFILENAMEA {aka tagOFNA*}' for argument '1' to 'BOOL GetOpenFileNameA(LPOPENFILENAMEA)'

means you should pass a pointer to OPENFILENAME as argument to GetOpenFileName.

error: conversion from 'BOOL {aka int}' to non-scalar type 'OPENFILENAME {aka tagOFNA}' requested

means the return type of GetOpenFileName should be BOOL.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490058

GetOpenFileName returns a BOOL, not an OPENFILENAME.

It will return a non-zero value if it returned by the user selecting a file and clicking "Ok". If they clicked "Cancel", it'll return 0.

If it returns true, it will have modified the contents of your open to reflect what the user selected.

So, you usually use it something like:

if (GetOpenFileName(&open)) {
    // use open.whatever to get data about the selected file
}
else
   // The user clicked cancel -- typically do nothing.

Upvotes: 4

Related Questions