Solved

Compile C++ project

Posted on 2003-12-04
6
1,010 Views
Last Modified: 2008-01-16
I have found some code that will be useful to a problem I am trying to tackle.
However I only have and know VB, not C++ and the code is C++.

Can anyone with C++ knowledge either compile the code to a dll that I can call from VB or translate it for use in a VB class.

The code is a wrapper to show the Format Dialog.

The code is from an article found at:

http://www.codeproject.com/dialog/cformatdrivedialog.asp?target=shformatdrive

Here is the contents of the FormatDrive.cpp file (copied from Notepad)
--------------------------------------------------------------------------------
/*
      Author - Nishant S
      EMail - nishforever@vsnl.com
*/

#include "StdAfx.h"
#include "formatdrive.h"

HHOOK CFormatDriveDialog::m_hHook=NULL;
LPCTSTR CFormatDriveDialog::m_vol_label = NULL;

CFormatDriveDialog::CFormatDriveDialog(void)
{
}

CFormatDriveDialog::~CFormatDriveDialog(void)
{
}

int CFormatDriveDialog::DoModal(HWND hWnd, UINT Drive, bool bQuickFormat,
                                                LPCTSTR vol_label)
{
      ASSERT(hWnd);
      m_vol_label = vol_label;
      int rv = 0;
      UINT Options = bQuickFormat ? SHFMT_OPT_QUICK : 0;
      FMTDRIVEFUNC* pshfd;
      HMODULE  hMod = LoadLibrary("shell32.dll");
      if(hMod)
      {
            pshfd = reinterpret_cast<FMTDRIVEFUNC*>(GetProcAddress(hMod,"SHFormatDrive"));
            m_hHook = SetWindowsHookEx(WH_CBT,CBTProc,
                  AfxGetApp()->m_hInstance,AfxGetApp()->m_nThreadID);
            rv = ((*pshfd)(hWnd,Drive,SHFMT_ID_DEFAULT,Options)==SHFMT_FMTSUCCESS) ? IDOK : IDCANCEL;
            FreeLibrary(hMod);
      }
      return rv;
}

LRESULT CALLBACK CFormatDriveDialog::CBTProc(int nCode,WPARAM wParam, LPARAM lParam)
{
      if (nCode == HCBT_ACTIVATE )
      {      
            HWND hWnd = reinterpret_cast<HWND>(wParam);
            
            EnumChildWindows(hWnd,EnumChildProc,NULL);      
            
            UnhookWindowsHookEx(m_hHook);
            CFormatDriveDialog::m_hHook = NULL;            
      }
      return FALSE;
}

BOOL CALLBACK CFormatDriveDialog::EnumChildProc( HWND hwnd, LPARAM lParam )
{      
      char buff[256];
      GetClassName(hwnd,buff,255);
      if(strcmp(buff,"Edit")==0)
      {
            SetWindowText(hwnd,m_vol_label);
            return FALSE;
      }
      return TRUE;
}



Here is the contents of the FormatDrive.h file:
--------------------------------------------------
/*
      Author - Nishant S
      EMail - nishforever@vsnl.com
*/

#pragma once

const UINT SHFMT_ID_DEFAULT  = 0xFFFF;

const UINT SHFMT_OPT_QUICK =  0x0001;

const UINT SHFMT_NOFMT  =  0xFFFFFFFF;  
const UINT SHFMT_FMTSUCCESS  =  0;  

const UINT DRIVEA = 0;
const UINT DRIVEB = 1;
const UINT DRIVEC = 2;
const UINT DRIVED = 3;
const UINT DRIVEE = 4;

class CFormatDriveDialog
{
public:
      CFormatDriveDialog(void);
      ~CFormatDriveDialog(void);      
      int DoModal(HWND , UINT , bool, LPCTSTR );
private:
      typedef  DWORD WINAPI FMTDRIVEFUNC (HWND, UINT,UINT,UINT);
      static LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam, LPARAM lParam);
      static BOOL CALLBACK EnumChildProc( HWND hwnd, LPARAM lParam );
      static HHOOK m_hHook;      
      static LPCTSTR m_vol_label;
};
0
Comment
Question by:holgrave
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
IF you want to use 'SHFormatDrive' from VB, why not using it directly? See http://www.mvps.org/vbnet/index.html?code/shell/undocshell32overview.htm

Declare Function SHFormatDrive Lib "shell32" _
(ByVal hwndOwner As Long, _
ByVal iDrive As Long, _
ByVal iCapacity As Long, _
ByVal iFormatType As Long) As Long

hOwner = Dialog owner; specify 0& for the desktop (will be top-level)
iDrive = The drive number to format. Drive A=0, B=1 (if present, otherwise C=1), and so on.
iCapacity = Can be the following values:

Public Const SHFD_CAPACITY_DEFAULT = 0 ' default drive capacity
Public Const SHFD_CAPACITY_360 = 3 ' 360KB, applies to 5.25" drives only
Public Const SHFD_CAPACITY_720 = 5 ' 720KB, applies to 3.5" drives only

iFormatType = Based on the OS, can be the following values:

WinNT
Public Const SHFD_FORMAT_FULL = 0 ' full format
Public Const SHFD_FORMAT_QUICK = 1 ' quick format

Win95
Public Const SHFD_FORMAT_QUICK = 0 ' quick format
Public Const SHFD_FORMAT_FULL = 1 ' full format
Public Const SHFD_FORMAT_SYSONLY = 2 ' copies system files only

Return vals: Operation cancelled by user: -2   ( - vbCancel )
                   Operation successful = 6   ( vbYes )

The return values do not indicate any options chosen by the user (Quick, No Label, Copy System Files), nor the type of format performed (Quick, Full, System Only)
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
BTW, if you still need it as a DLL, drop me an email. The full VB sample can be found at http://www.mvps.org/vbnet/index.html?code/shell/undocformatdlg.htm

UndocSHFormat.bas:

--------------------------------------------------------------------------------
 
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce
'               or publish this code on any web site,
'               online service, or distribute as source
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
------------------------------------------------------
'Determines if the current OS is WinNT.
'Tested in the form load event only
Public Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
  (lpVersionInformation As OSVERSIONINFO) As Long
                                     
Public Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128
End Type
   
Public Const VER_PLATFORM_WIN32s = 0
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const VER_PLATFORM_WIN32_NT = 2

'APIs declarations for the format function
Public Declare Function SHFormatDrive Lib "shell32" _
  (ByVal hwndOwner As Long, _
   ByVal iDrive As Long, _
   ByVal iCapacity As Long, _
   ByVal iFormatType As Long) As Long
   
Public Const SHFD_CAPACITY_DEFAULT = 0 'default drive capacity
Public Const SHFD_CAPACITY_360 = 3     '360KB, 5.25" only
Public Const SHFD_CAPACITY_720 = 5     '720KB, 3.5" only
 
'Normally, these below would be declared as constants,
'if the values between Win95/98 and NT were the same (see
'the commented-out const values below).
Public SHFD_FORMAT_QUICK As Long
Public SHFD_FORMAT_FULL As Long
Public SHFD_FORMAT_SYSONLY As Long
 
'However, because VB lacks a conditional #If variable
'to distinguish between Win95/98 and NT, they are DIMmed
'here and set in the Form_Load routine after GetVersionEx
'has been called.
 
'Win95/98
'Public Const SHFD_FORMAT_QUICK = 0   'quick format
'Public Const SHFD_FORMAT_FULL = 1    'full format
'Public Const SHFD_FORMAT_SYSONLY = 2 'copy sys files only
   
'WinNT
'Public Const SHFD_FORMAT_FULL = 0   'full format
'Public Const SHFD_FORMAT_QUICK = 1  'quick format
     
'------------------------------------------------------
'support APIs to populate the combo box
'with the available drives
 
Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
    Alias "GetLogicalDriveStringsA" _
   (ByVal nBufferLength As Long, _
    ByVal lpBuffer As String) As Long
   
Public Declare Function GetDriveType Lib "kernel32" _
   Alias "GetDriveTypeA" _
  (ByVal nDrive As String) As Long
   
Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6
   

Public Function IsWinNT() As Boolean

  'Returns True if the current operating
  'system is WinNT
   Dim osvi As OSVERSIONINFO
   osvi.dwOSVersionInfoSize = Len(osvi)
   GetVersionEx osvi
   IsWinNT = (osvi.dwPlatformId = VER_PLATFORM_WIN32_NT)
   
End Function
'--end block--'
 
0
 
LVL 1

Author Comment

by:holgrave
Comment Utility
Thanks
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:holgrave
Comment Utility
BTW jkr.

I've used what you posted and the Randy Birch example to do my own version. But I am also trying to hook the Format Dialog so I can move it to where I want it to appear on the screen.

The hooking doesn't seem to work - even though I have got it to work before under Win9x.

Under Win9x the CBTHook routine stores the Format Dialog handle in wParam.
I think it must be different under NT because when I use wParam as the handle for the dialog here and pass it to the MoveWindow routine it get a big CRASH!!

Any ideas??

Here is the code:
--------------------------------------------------------------------
Option Explicit

'Ed
Public Enum FormatType
   ftFull = 0
   ftQuick = 1
   ftSysOnly = 2
End Enum

'DriveID is a 0 based index with A=0, B=1 etc.
Private Const DRIVE_A = 0


'Win95:
'Return values for SHFormatDrive operation
'-3 failure, drive can't be formatted (either doesn't exist or contains files in use by Windows)
'-2 operation cancelled (vbCancel)
'-1 operation attempted but completed unsuccessfully
'6(?) operation completed successfully (vbYes)

'
'WinNT:
'B. Martinez
'-1 dialog was shown
'0 error, dialog not shown

'Ed
'In my experience it is:
'-1 failed/cancelled
'0 successful!

'Ed Hook Stuff
'--------------------------------
Private hHook As Long

Const WH_CBT = 5

Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long

Const HCBT_ACTIVATE = 5

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'----- end hook stuff ----------------------


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce
'               or publish this code on any web site,
'               online service, or distribute as source
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'------------------------------------------------------

'Determines if the current OS is WinNT.
'Tested in the form load event only
Public Declare Function GetVersionEx Lib "kernel32" _
   Alias "GetVersionExA" _
  (lpVersionInformation As OSVERSIONINFO) As Long
                                     
Public Type OSVERSIONINFO
   dwOSVersionInfoSize As Long
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformId As Long
   szCSDVersion As String * 128
End Type
   
Public Const VER_PLATFORM_WIN32s = 0
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const VER_PLATFORM_WIN32_NT = 2

'APIs declarations for the format function
Public Declare Function SHFormatDrive Lib "shell32" _
  (ByVal hWndOwner As Long, _
   ByVal iDrive As Long, _
   ByVal iCapacity As Long, _
   ByVal iFormatType As Long) As Long
   
Public Const SHFD_CAPACITY_DEFAULT = 0 'default drive capacity
Public Const SHFD_CAPACITY_360 = 3     '360KB, 5.25" only
Public Const SHFD_CAPACITY_720 = 5     '720KB, 3.5" only
 
'Normally, these below would be declared as constants,
'if the values between Win95/98 and NT were the same (see
'the commented-out const values below).
Public SHFD_FORMAT_QUICK As Long
Public SHFD_FORMAT_FULL As Long
Public SHFD_FORMAT_SYSONLY As Long
 
'However, because VB lacks a conditional #If variable
'to distinguish between Win95/98 and NT, they are DIMmed
'here and set in the Form_Load routine after GetVersionEx
'has been called.
 
'Win95/98
'Public Const SHFD_FORMAT_QUICK = 0   'quick format
'Public Const SHFD_FORMAT_FULL = 1    'full format
'Public Const SHFD_FORMAT_SYSONLY = 2 'copy sys files only
   
'WinNT
'Public Const SHFD_FORMAT_FULL = 0   'full format
'Public Const SHFD_FORMAT_QUICK = 1  'quick format


Public Function IsWinNT() As Boolean

  'Returns True if the current operating
  'system is WinNT
   Dim osvi As OSVERSIONINFO
   osvi.dwOSVersionInfoSize = Len(osvi)
   GetVersionEx osvi
   IsWinNT = (osvi.dwPlatformId = VER_PLATFORM_WIN32_NT)
   
End Function

'Public Function FormatFloppy(hWndOwner As Long, Format As FormatType) As Boolean

Public Function FormatFloppy(Format As FormatType) As Boolean
Dim lgReturn As Long
Dim hWndParent As Long

   ' Start the CBT hook
   hHook = SetWindowsHookEx(WH_CBT, AddressOf CBTHook, App.hInstance, 0)

   'set desktop as the format diaLOg's parent - stops problems with setting the focus to the custom frmat dialog (see Hook)
   hWndParent = 0
   
   If IsWinNT Then
      SHFD_FORMAT_FULL = 0    'full format
      SHFD_FORMAT_QUICK = 1   'quick format
     
      If Format = ftFull Then
         lgReturn = SHFormatDrive(hWndParent, DRIVE_A, SHFD_CAPACITY_DEFAULT, SHFD_FORMAT_FULL)
      Else
         lgReturn = SHFormatDrive(hWndParent, DRIVE_A, SHFD_CAPACITY_DEFAULT, SHFD_FORMAT_QUICK)
      End If
     
      If lgReturn = 0 Then FormatFloppy = True
     
   Else 'it's Win95/98
      SHFD_FORMAT_QUICK = 0   'quick format
      SHFD_FORMAT_FULL = 1    'full format
      SHFD_FORMAT_SYSONLY = 2 'copies system files only
     
      If Format = ftFull Then
         lgReturn = SHFormatDrive(hWndParent, DRIVE_A, SHFD_CAPACITY_DEFAULT, SHFD_FORMAT_FULL)
      Else
         lgReturn = SHFormatDrive(hWndParent, DRIVE_A, SHFD_CAPACITY_DEFAULT, SHFD_FORMAT_QUICK)
      End If
     
      If lgReturn = 6 Then FormatFloppy = True
     
   End If

End Function

'Floppy Hook
Private Function CBTHook(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Start As Long, Child As Long, hWndFormat As Long

   On Error GoTo ErrorHandler

   Select Case Code
      Case HCBT_ACTIVATE

         ' Destroy this hook
         UnhookWindowsHookEx hHook

         ' Get the format dlg hWnd
         hWndFormat = wParam

         MoveWindow hWndFormat, 0, 0

    Case Else

    End Select

   Exit Function
   
ErrorHandler:
   Debug.Print Err.Number & ": " & Err.Description
End Function


0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>Any ideas??

Um, I do know *nothing* about VB except that I once neede that funcionality for C++ and found it on that MVP site to translate it the other way round...
0
 
LVL 1

Author Comment

by:holgrave
Comment Utility
OK thanks for getting me this far..... :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

11 Experts available now in Live!

Get 1:1 Help Now