Solved

How to write declaration myself to use Comdlg32.dll?

Posted on 2001-07-13
15
429 Views
Last Modified: 2012-05-04
I have a case I need to use Save As dialog (Microsoft Common Dialog -- comdlg32.dll) allowing user to choose a folder from an application (Docbasic, which is similar to VB but not VB). Please note:

1) I have to use comdlg32.dll, not comdlg32.ocx (ActiveX control). I can't use ActiveX control in Docbasic.

2) I need a handle when using the dll Save As dialog -- I need to pass filename to the filename field in the dialog. User doesn't need to type in filename when the Save As dialog pops up. And I also need to catch the folder path that user selected. So only launching the dialog without having a handle would not work.

3) VB has a predefined declaration for using this dll dialog for VB applications in a file called Win32api.txt. The declaration is like this:

-- begin

Private Type OPENFILENAME
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
End Type

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

-- end

Then I can call the function from a VB application. it works fine. Now my question is: how can I do the similar thing myself in another language?

Thank you very much!
0
Comment
Question by:yzmao
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6281042
What language?
0
 

Author Comment

by:yzmao
ID: 6281419
Docbasic. A language for Documentum (a knowledge management software).
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6282517
Does it support APIs? What format is an API in (post one!)?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6282678
Interesting... give us more info.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6283142
You will have to check the documentation for Docbasic to see if it supports calls to DLLs (not Active X DLLs, but the function based kind.)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6284155
"I have a case I need to use Save As dialog (Microsoft Common Dialog -- comdlg32.dll) allowing user to
choose a folder from an application "
did you try with browseforfolders stuff, it is pretty easy.
0
 

Author Comment

by:yzmao
ID: 6285828
Hi,

Thanks for everyone!

Does it support APIs?

  -- Yes. The declaration is similar to(if not exactly as) VB. But I do have a problem here: I found that the structure name OPENFILENAME is a reserved word in Docbasic. So I can't use it to declare the structure I need. Do I hit a dead end?

Docbasic documentation:

  -- Of course I checked all documentation. Unfortunately, nothing I could find for further instruction. It is obvious that I have to do something meself -- with the help from your guys.

 "did you try with browseforfolders stuff, it is pretty easy" -- what is this? Would you please give more info?

Note: Docbasic is similar to VB (with less functionality, of course). So approaches in VB (not ActiveX) to Windows API might work.

Thanks a lot!
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6286547
<<... the structure name OPENFILENAME is a reserved word ...>>

So change it:

Private Type VBOPENFILENAME

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename
As VBOPENFILENAME) As Long
0
 

Author Comment

by:yzmao
ID: 6286577
I did -- I mean I used another name for the structure. The Docbasic program runs, no error -- the return value was also correct, but the Save As dialog was not launched. I didn't know what was wrong.

So you mean the name of the structure doesn't matter?
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6286746
That's correct. The structure name is irrelevant, as are the names of parameters and the API itself (though Alias and Lib must be left alone!). It doesn't even exist in the compiled exe (unless you are exporting the structure), it's just an easy way to refer to memory locations when you are writing source and is converted to memory pointers in the final exe. In fact, this will work just fine:

Private Type Yeah_Whatever

Private Declare Function DoThatThing Lib "comdlg32.dll" Alias "GetSaveFileNameA" (Alright As Yeah_Whatever) As Long


There is an API you can use after the call to see if there was an error:

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

An easy way to use it is contained in AllAPI's API-Guide (I made a few minor changes, but nothing functional):

==================
Const CDERR_DIALOGFAILURE = &HFFFF
Const CDERR_FINDRESFAILURE = &H6
Const CDERR_GENERALCODES = &H0
Const CDERR_INITIALIZATION = &H2
Const CDERR_LOADRESFAILURE = &H7
Const CDERR_LOADSTRFAILURE = &H5
Const CDERR_LOCKRESFAILURE = &H8
Const CDERR_MEMALLOCFAILURE = &H9
Const CDERR_MEMLOCKFAILURE = &HA
Const CDERR_NOHINSTANCE = &H4
Const CDERR_NOHOOK = &HB
Const CDERR_REGISTERMSGFAIL = &HC
Const CDERR_NOTEMPLATE = &H3
Const CDERR_STRUCTSIZE = &H1
Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Private Sub ShowError()

    GetOpenFileName ByVal 0&
    Select Case CommDlgExtendedError
        Case CDERR_DIALOGFAILURE
            MsgBox "The dialog box could not be created."
        Case CDERR_FINDRESFAILURE
            MsgBox "The common dialog box function failed to find a specified resource."
        Case CDERR_INITIALIZATION
            MsgBox "The common dialog box function failed during initialization."
        Case CDERR_LOADRESFAILURE
            MsgBox "The common dialog box function failed to load a specified resource."
        Case CDERR_LOADSTRFAILURE
            MsgBox "The common dialog box function failed to load a specified string."
        Case CDERR_LOCKRESFAILURE
            MsgBox "The common dialog box function failed to lock a specified resource."
        Case CDERR_MEMALLOCFAILURE
            MsgBox "The common dialog box function was unable to allocate memory for internal structures."
        Case CDERR_MEMLOCKFAILURE
            MsgBox "The common dialog box function was unable to lock the memory associated with a handle."
        Case CDERR_NOHINSTANCE
            MsgBox "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle."
        Case CDERR_NOHOOK
            MsgBox "The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure."
        Case CDERR_REGISTERMSGFAIL
            MsgBox "The RegisterWindowMessage function returned an error code when it was called by the common dialog box function."
        Case CDERR_NOTEMPLATE
            MsgBox "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template."
        Case CDERR_STRUCTSIZE
            MsgBox "The lStructSize member of the initialization structure for the corresponding common dialog box is invalid."
        Case Else
            MsgBox "Undefined error ..."
    End Select

End Sub
================

This might tell you what went wrong with your call and give you a chance to fix your code.

Good luck!
0
 
LVL 5

Accepted Solution

by:
KDivad earned 300 total points
ID: 6286755
Oops! Remove the GetOpenFileName line from that sub. The API-Guide used it to force an error so that the select case would show something.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6287704
were you declaring that variable of type:VBOPENFILENAME?
if so, were you declaraing API with this newname as parameter type?
If so, were you pass that var to openfilename function?
0
 

Author Comment

by:yzmao
ID: 6291035
Thanks for everyone!
0
 
LVL 5

Expert Comment

by:KDivad
ID: 6291054
You're welcome!

I assume you got it working, then?
0
 

Author Comment

by:yzmao
ID: 6295634
Yes, I have tested in VB, as you said the name of the structure is irrelevant. But I still have problem in Docbasic, that's my own problem now -- I mean the software problem.

You are pretty good.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month4 days, 9 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question