Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

My first ever example ported from C++ to VB6 as I interperated it with a little touch

$
0
0
I felt for doing my very first interperation of a sample from the Win7 SDK "strait out from the comfort."
I did NOT peep at any other VB6 code while I did this. Just soly relied on the C++ source and my knowlewdge from the "early days" in COM and Shell32 coding. And for good reason I did left this out this API...TaskDialogIndirect which was included in this C++ sample because Fafalone have already enough made very great and extensive samples about this API so I left this out for his credit and fellowship because he is a GOOD guy ;)

My sample deals about the combination of three COM interfaces and theese are IStorage, Istream and IShellItem.

This sample utilizes theese three interfaces in a very nice harmoic way.

The sample deals about creating files and folders in the shells's namespace and utilize the Explorer's built in dialogs.

In this sample it deals about creating a file and a folder into a Documents Library.

This first sample creates a file and writes data to it and also creates a folder within a documents library.

This is done "directly without any dialogboxes" just to show how its done without them.

Code:

Public Function ExportToDocumentsLibrary(ByVal sSampleFolderName As String, ByVal sSampleFileName As String, ByVal sSampleFileContents As String) As Long
  Dim pISI As IShellItem
  Dim hr As Long
  Static FolderId As GUID
  Dim pName As Long
 
  'Documents Library
  IIDFromString2 StrPtr("{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}"), FolderId
 
  hr = SHCreateItemInKnownFolder(FolderId, KF_FLAG_DEFAULT, 0&, IID_IShellItem, pISI)
 
  'Vista does not have a Documents Library, obvius not Win10 as well so use the Documents Folder if it fails...
  If hr <> S_OK Then
    'Documents Folder
    IIDFromString2 StrPtr("{FDD39AD0-238F-46AF-ADB4-6C85480369C7}"), FolderId
    hr = SHCreateItemInKnownFolder(FolderId, KF_FLAG_DEFAULT, 0&, IID_IShellItem, pISI)
    pISI.GetDisplayName SIGDN_DESKTOPABSOLUTEPARSING, pName
    Debug.Print GetStrAFromStrPtrW(pName)
  End If
 
 If hr = S_OK Then
    hr = CreateFileInContainer(pISI, sSampleFileName, sSampleFileContents)
    If hr = S_OK Then
      hr = CreateFolderInContainer(pISI, sSampleFolderName)
      Form1.Text2.Text = szSampleFileName
      Form1.Text3.Text = sSampleFolderName
    End If
  End If
  ExportToDocumentsLibrary = hr
End Function

Next step is to put this into "containers" in other words existing folders using dialogs helping you to browse to theese "containers" (Folders) - ** in contrast from the orginal sample I have preselected by default the folder I use in this sample.

First out is the "FilePicker"


Code:

Public Function ExportToFileDialogItem()
  Dim hr As Long
  Dim pIFSD As IFileSaveDialog
  Dim rgSaveTypes() As COMDLG_FILTERSPEC
  Dim pISI As IShellItem
  Dim pISI_DefFldr As IShellItem '** <-- This is the addition
  Dim n As Long
 
  hr = ShGetIFileSaveDialog(pIFSD)
 
  If hr = S_OK Then
    hr = SHCreateItemFromParsingName(StrPtr(Form1.Text1.Text), 0&, IID_IShellItem, pISI_DefFldr) '** <-- This is the addition
    If hr = S_OK Then
      pIFSD.SetDefaultFolder pISI_DefFldr '** <-- This is the addition
      pIFSD.SetFolder pISI_DefFldr
    End If
    pIFSD.SetDefaultExtension "All Files,*.*"
    pIFSD.SetFileName c_szSampleFileName
    ReDim rgSaveTypes(1)
    '**Here I'm real bad on fixing this!!
    For n = 0 To 1 'UBound(rgSaveTypes)
      rgSaveTypes(0).pszSpec = "All Files,*.*"
      rgSaveTypes(1).pszSpec = "Text File,*.txt;*.html"
      rgSaveTypes(0).pszName = "All File"
      rgSaveTypes(1).pszName = "Text Files"
      pIFSD.SetFileTypes n, VarPtr(rgSaveTypes(n))
    Next n
    pIFSD.Show 0
    pIFSD.GetResult pISI
    hr = CreateFileFromItem(pISI, c_szSampleFileContents)
    Set pISI = Nothing
    Set pISI_DefFldr = Nothing
    Set pIFSD = Nothing
  End If
 
**(I can directly give myself negative critisism because I'm really bad in converting C++ string arrays to VB6 working string arrays).

Next is the "Folder Picker" which lets you select a folder in same manner as in "File Picker" but now it's modified to just selecting folders - ** in contrast from the orginal sample I have preselected by default the folder I use in this sample.

Code:

Public Function ExportToFolderPicker() As Long
  Dim pfod As IFileOpenDialog
  Dim hr As Long
  Dim pISI As IShellItem
  Dim pISI_DefFldr As IShellItem '** <-- This is the addition
 
  hr = ShGetIFileOpenDialog(pfod)
 
  If hr = S_OK Then
    hr = SHCreateItemFromParsingName(pName, 0&, IID_IShellItem, pISI_DefFldr) '** <-- This is the addition
    pfod.SetOptions FOS_PICKFOLDERS Or FOS_FORCESHOWHIDDEN
    pfod.SetDefaultFolder pISI_DefFldr '** <-- This is the addition
    pfod.Show 0
    pfod.GetResult pISI
    hr = CreateFileInContainer(pISI, c_szSampleFileName, c_szSampleFileContents)
    If hr = S_OK Then
      hr = CreateFolderInContainer(pISI, c_szSampleFolderName)
    End If
    Set pISI = Nothing
  End If
  Set pfod = Nothing
End Function

And the third and last sample in my example is something called "BrowseForFolder" and this dialog is in contrast to the the other two dialoges NOT an Interface!! Why it's like this I don't know but this is a API called BrowseForFolder.
I will not break down this further so therefore I pass this to the MSDN Documatation.


I did a little twist on this just for fun to "spice it up!
Here we go...

Code:

'Here is the optional thing I mentioned.
g_sOkButton = InputBox("Hell Yea!" & vbCrLf & "Now you have the option to rename the OK button in the Browse For Folder dialog. If you leave the text field blank there will be 'OK'.", "Set your custom name for OK button. (You can choose anthother title :)")

'This is the main thing.
Public Function ExportToSHBrowseForFolder() As Long
  Dim lpBI As BROWSWINFO
  Dim pidl As Long
  Dim pISI As IShellItem
  Dim hr As Long
 
  lpBI.ulFlags = BIF_USENEWUI Or BIF_STATUSTEXT Or BIF_EDITBOX
  lpBI.lpCallBack = BFFProc(AddressOf BrowseCallBackProc)
  lpBI.pidlRoot = VarPtr(0)
 
  pidl = SHBrowseForFolder(lpBI)
 
  If pidl > 0 Then
    hr = SHCreateItemFromIDList(pidl, IID_IShellItem, pISI)
    If hr = S_OK Then
      hr = CreateFileInContainer(pISI, c_szSampleFileName, c_szSampleFileContents)
      If hr = S_OK Then
        hr = CreateFolderInContainer(pISI, c_szSampleFolderName)
      End If
      Set pISI = Nothing
    End If
    CoTaskMemFree pidl
  End If
  ExportToSHBrowseForFolder = hr
End Function

I have not tried yet if theese three interfaces can create a "virtual path" as Desktop with as CLSID insted of a real path.
Full Sourcecode is in the zipfile!

Credit goes in first hand to Mr Fafalone because I told him I shall practice one year to reclame old knowledge and relearn new stuff since my 15 years lacking so he have really learned me new stuffs and new aproaches. ;)

Second Credit goes to VanGoghGaming who have given my new insights in vb6 distributable API's :)

Thrid Credit goes to wqweto who have given me insights in great michlangelous stuff :duck:

And you need to use oleexp.tbl for this to work!!
Attached Images
    
Attached Files

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>