Jump to content

Help with GetOpenFileName

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Hi,

Im trying to make a simple script whereby a user browses for a file, then this location can then be used in a variable. Can anyone tell me how this code can be used, and if it is C and not C# or C++.

Ive found several examples on the internet, but if I try to compile these examples I get lots of error codes saying it isnt correctly coded.

EDIT: For reference this is the code that I found that didnt compile:


#include <windows.h>

#include <fstream>

#include <stdafx.h>


int main()

{

printf("declaring...\n");

// Declare

OPENFILENAME ofn;       // common dialog box structure

char szFile[260];       // buffer for file name

HWND hwnd;              // owner window

HANDLE hf;              // file handle


printf("initialising...\n");

// Initialize OPENFILENAME

ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);

ofn.hwndOwner = hwnd;

ofn.lpstrFile = szFile;

//

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 

// use the contents of szFile to initialize itself.

//

printf("setting...\n");

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;


printf("displaying...\n");

// Display the Open dialog box. 

if (GetOpenFileName(&ofn)==TRUE) 

    hf = CreateFile(ofn.lpstrFile, GENERIC_READ,

        0, (LPSECURITY_ATTRIBUTES) NULL,

        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,

        (HANDLE) NULL);


printf("\n");

printf("opened. exiting...\n");

printf("...");


return 0;

}#include <windows.h>

#include <fstream>


int main()

{

printf("declaring...\n");

// Declare

OPENFILENAME ofn;       // common dialog box structure

char szFile[260];       // buffer for file name

HWND hwnd;              // owner window

HANDLE hf;              // file handle


printf("initialising...\n");

// Initialize OPENFILENAME

ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);

ofn.hwndOwner = hwnd;

ofn.lpstrFile = szFile;

//

// Set lpstrFile[0] to '\0' so that <strong class="highlight">GetOpenFileName</strong> does not 

// use the contents of szFile to initialize itself.

//

printf("setting...\n");

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;


printf("displaying...\n");

// Display the Open dialog box. 

if (<strong class="highlight">GetOpenFileName</strong>(&ofn)==TRUE) 

    hf = CreateFile(ofn.lpstrFile, GENERIC_READ,

        0, (LPSECURITY_ATTRIBUTES) NULL,

        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,

        (HANDLE) NULL);


printf("\n");

printf("opened. exiting...\n");

printf("...");


return 0;

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
This appears to be C code, but may be C++. How are you trying to compile it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,714 posts
EDIT: I figured it out. You have to have your variable declarations at the top of each function before any function calls or statements. Get rid of your "declaring..." printf and you'll be fine. Your code has some other flaws, however: You use hwnd without initializing it (line 16). This can cause problems later on in your code.

Edited by dargueta, 05 February 2009 - 11:30 AM.
Figured out problem.