Archive for the ‘Undocumented WinAPI’ Category

WinAPI Wrapper for GetOpenFileName

November 21, 2007 Leave a comment

Shell32.dll provides an undocumented API that wraps around a call to GetOpenFileName called GetFileNameFromBrowse we’ll have to get the call pointer using GetProcAddress, quite easy to use! ūüėČ Well I can see the documentation in MSDN but can’t find the prototype in any of the¬†header file in my SDK folder, so till then GetProcAddress is our refuge. As per MSDN prototype can be found in shlobj.h…

int _tmain()
    // Creates a typedef
    typedef BOOL ( WINAPI *GetFileNameFromBrowse )( HWND hwnd,
                                                    LPWSTR pszFilePath,
                                                    UINT cchFilePath,
                                                    LPCWSTR pszWorkingDir,
                                                    LPCWSTR pszDefExt,
                                                    LPCWSTR pszFilters,
                                                    LPCWSTR szTitle );   

    // Path buffer, specifies starting directory and on successful return
    // from open dialog also holds the selected file name
    wchar_t wszPath[MAX_PATH] = L"D:\\";   

    // Load shell32 dll
    HMODULE hModule = LoadLibrary( "Shell32.dll" );
    if( !hModule )
       return 1;

    // Get procedure address
    GetFileNameFromBrowse GetFileNameFromBrowsePtr = ( GetFileNameFromBrowse )GetProcAddress( hModule, "GetFileNameFromBrowse" );   

    // Show browse dialog
    if( GetFileNameFromBrowsePtr && GetFileNameFromBrowsePtr( 0, wszPath, MAX_PATH, 0, 0, L"*.*", L"Nibu Open" ))
       MessageBoxW( 0, wszPath, L"You selected", MB_OK | MB_ICONINFORMATION );

    // Free loaded library
    FreeLibrary( hModule );   

    return 0;   

}// End _tmain

Changing IP Address of a machine!

October 13, 2007 Leave a comment

To change ip address of a machine use SetAdapterIpAddress. It’s an undocumented API found in iphlpapi.dll.

Function signature is as follows…

DWORD SetAdapterIpAddress( char *pszAdapGUID,
                           DWORD dwDHCP,
                           DWORD dwIP,
                           DWORD dwMask,
                           DWORD dwGateway );

Look here for a demo, it’s in Russian but code is in english ūüėČ

Showing “Select Computer” dialog, with all network computers listed out!

September 20, 2007 Leave a comment

Ever seen that “Select Computer” dialog coming up and did you wonder how¬†to have all those computer names without writing much code. There is an undocumented exported api in “ntlanman.dll” called ServerBrowseDialogA0 which is used exactly for this purpose.

int main()
// Some funky stuff
¬†¬†¬† HMODULE hMod = LoadLibrary( “Kernel32.dll” );
¬†¬†¬† GetConsoleWindow GCW = (GetConsoleWindow)GetProcAddress( hMod, “GetConsoleWindow” );
    HWND hConsole = GCW();
¬†¬†¬† SetWindowText( hConsole, “Nibu” );

// The real stuff for showing the dialog starts here
¬†¬†¬† HMODULE hModule = LoadLibrary( _T( “ntlanman.dll” ));

    typedef DWORD ( WINAPI *FNNTBrowseDlg )( HWND hwnd,
                                             CHAR *pchBuffer,
                                             DWORD cchBufSize );
    FNNTBrowseDlg lpfn = 0;
    CHAR szT[MAX_PATH + 1] = { 0 };
¬†¬†¬† lpfn = ( FNNTBrowseDlg )GetProcAddress( hModule, “ServerBrowseDialogA0” );

    // Will return zero on success, now show the dialog
    const DWORD dwResult = lpfn( hConsole, szT, MAX_PATH );

// Check result
    if( !dwResult )
      stringstream sStream;
      sStream < < "Selected computer name is: " << szT;       MessageBox( hConsole, sStream.str().c_str(), "Computer name", MB_OK | MB_ICONINFORMATION );     }     else     {       MessageBox( hConsole, "You didn't make any selection", "Computer name", MB_OK | MB_ICONINFORMATION );     }     FreeLibrary( hModule );     FreeLibrary( hMod ); return 0; }// End main[/sourcecode] Output:

Changing console application window icon at runtime!

July 24, 2007 19 comments

Bored with the old black icon that you have on the main window. Here is a way to change it. There is an undocumented API called SetConsoleIcon and it’s in Kernel32.dll.¬† Here is how we use it…

void ChangeIcon( const HICON hNewIcon )
   // Load kernel 32 library
   HMODULE hMod = LoadLibrary( _T( "Kernel32.dll" ));
   ASSERT( hMod ); 

   // Load console icon changing procedure
   typedef DWORD ( __stdcall *SCI )( HICON );
   SCI pfnSetConsoleIcon = reinterpret_cast<sci>( GetProcAddress( hMod, "SetConsoleIcon" ));
   ASSERT( pfnSetConsoleIcon ); 

   // Call function to change icon
   pfnSetConsoleIcon( hNewIcon ); 

   FreeLibrary( hMod );
}// End ChangeIcon 

// Main function
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
   int nRetCode = 0; 

   HMODULE hMainMod = GetModuleHandle( 0 );
   ASSERT( hMainMod );

   HICON hMainIcon = ::LoadIcon( hMainMod, MAKEINTRESOURCE( IDI_ICON3 ));
   ASSERT( hMainIcon ); 

   // Change main window icon
   ChangeIcon( hMainIcon );

   // To reset to old icon uncomment this code
   // ChangeIcon( 0 );

   return nRetCode;


%d bloggers like this: