Solved

How to write declaration myself to use Comdlg32.dll?

Posted on 2001-07-13
15
423 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now