Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Shortcuts for mounted devices on desktop

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Tue Mar 17, 2015 6:40 pm    Post subject: Shortcuts for mounted devices on desktop Reply with quote

Working with a Mac for a couple of weeks made me get used to all mounted drives/network shares being placed on the desktop, which is an awesome feature. I decided i couldn't live without this feature, so i created a small utility to add this functionality to Windows.

In case anyone else might be interested, i thought i might share the source code. I really hate the fact that the shortcuts are created using COM, but it was the only information i could find on MSDN. If anyone knows a way to create shortcuts without the COM library, feel free to share. The rest is written in very little time, just to get it working.

main.cpp:
Code:
#include "DiskShortcuts.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
   DiskShortcuts *diskShortcuts = new DiskShortcuts(hInstance);
   diskShortcuts->RegisterMainClass();
   diskShortcuts->CreateMainWindow(nCmdShow);

   delete diskShortcuts;
}


DiskShortcuts.h:
Code:
#ifndef DISKSHORTCUTS_H
#define DISKSHORTCUTS_H

#include <Windows.h>
#include <string>
#include <Dbt.h>

class DiskShortcuts{
public:
   DiskShortcuts(HINSTANCE hInstance);
   ~DiskShortcuts();

   int RegisterMainClass();
   int CreateMainWindow(int nCmdShow);

   int CreateShortcut(char* dir);
   int InitializeShortcuts();

   LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
private:
   static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
   HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc);
   std::string shortcuts[256];

protected:
   HWND hWnd;
   HINSTANCE hInstance;
};

#endif


DiskShortcuts.cpp:
Code:
#include <stdio.h>

#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"

#include "DiskShortcuts.h"

LRESULT CALLBACK DiskShortcuts::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
   DiskShortcuts *diskShortcuts;
   if (msg == WM_CREATE){
      CREATESTRUCT *createstruct;
      createstruct = (CREATESTRUCT*)lParam;
      SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG)createstruct->lpCreateParams);
      diskShortcuts = (DiskShortcuts*)createstruct->lpCreateParams;
   }
   else{
      diskShortcuts = (DiskShortcuts*)GetWindowLongPtr(hWnd, GWL_USERDATA);
   }

   return diskShortcuts->WindowProcedure(hWnd, msg, wParam, lParam);
}

LRESULT CALLBACK DiskShortcuts::WindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
   switch (msg){
   case WM_CREATE:
      InitializeShortcuts();
      break;
   case WM_CLOSE:
      for (int i = 0; i < 255; i++){
         if (shortcuts[i] != ""){
            remove(shortcuts[i].c_str());
         }
      }
      DestroyWindow(hWnd);
      break;
   case WM_DESTROY:
      PostQuitMessage(0);
      break;
   case WM_ENDSESSION:
      if (wParam){
         for (int i = 0; i < 255; i++){
            if (shortcuts[i] != ""){
               remove(shortcuts[i].c_str());
            }
         }
      }
      break;
   case WM_DEVICECHANGE:
      switch (wParam)
      {
      case DBT_DEVICEARRIVAL:
      {
         PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lParam;
         char i;
         ULONG unitmask = lpdbv->dbcv_unitmask;
         for (i = 0; i < 26; ++i){
            if (unitmask & 0x1)
               break;
            unitmask = unitmask >> 1;
         }
         i += 'A';
         char dir[4];
         sprintf_s(dir, 4, "%c:\\", i);
         CreateShortcut(dir);
      }
      break;
      case DBT_DEVICEREMOVECOMPLETE:
      {
         PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lParam;
         char i;
         ULONG unitmask = lpdbv->dbcv_unitmask;
         for (i = 0; i < 26; ++i){
            if (unitmask & 0x1)
               break;
            unitmask = unitmask >> 1;
         }
         i += 'A';
         remove(shortcuts[i].c_str());
         shortcuts[i] = "";
      }
      break;
      default:
         break;
      }
   }
   return DefWindowProc(hWnd, msg, wParam, lParam);
}

DiskShortcuts::DiskShortcuts(HINSTANCE hInst){
   hInstance = hInst;

   CoInitialize(NULL);
}

DiskShortcuts::~DiskShortcuts(){
   for (int i = 0; i < 255; i++){
      if (shortcuts[i] != ""){
         remove(shortcuts[i].c_str());
      }
   }
}

int DiskShortcuts::RegisterMainClass(){
   WNDCLASSEX wc;

   wc.cbSize = sizeof(WNDCLASSEX);
   wc.style = 0;
   wc.lpfnWndProc = this->WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInstance;
   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
   wc.lpszMenuName = NULL;
   wc.lpszClassName = "clsDiskShortcuts";
   wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

   if (!RegisterClassEx(&wc)){
      MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
      return 1;
   }

   return 0;
}

int DiskShortcuts::CreateMainWindow(int nCmdShow){
   HWND hWnd;
   MSG Msg;

   hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "clsDiskShortcuts", "Disk Shortcuts", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, this);
   
   if (hWnd == NULL){
      MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
      return 0;
   }

   ShowWindow(hWnd, SW_HIDE);
   //ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   while (GetMessage(&Msg, NULL, 0, 0) > 0){
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }
   return Msg.wParam;
}

int DiskShortcuts::CreateShortcut(char* dir){
   char *path = new char[512];
   char volname[512];

   if (shortcuts[dir[0]] != "")
      return -1;

   GetVolumeInformation(dir, volname, 512, NULL, NULL, NULL, NULL, 0);
   if (volname[0] == '\0'){
      switch (GetDriveType(dir)){
      case DRIVE_UNKNOWN:
         sprintf_s(volname, 512, "Unkown Drive");
         break;
      case DRIVE_NO_ROOT_DIR:
         sprintf_s(volname, 512, "Directory");
         break;
      case DRIVE_REMOVABLE:
         sprintf_s(volname, 512, "Removable Disk");
         break;
      case DRIVE_FIXED:
         sprintf_s(volname, 512, "Local Disk");
         break;
      case DRIVE_REMOTE:
         sprintf_s(volname, 512, "Remote Drive");
         break;
      case DRIVE_CDROM:
         sprintf_s(volname, 512, "CD Drive");
         break;
      case DRIVE_RAMDISK:
         sprintf_s(volname, 512, "Ram Disk");
         break;
      default:
         break;
      }
   }
   sprintf_s(path, 512, "%s\\%s (%c).lnk", "C:\\Users\\Anders\\Desktop", volname, dir[0]);
   shortcuts[dir[0]] = path;
   CreateLink(dir, path, "");

   return 0;
}

int DiskShortcuts::InitializeShortcuts(){
   char buf[512];
   char *pbuf = buf;
   
   GetLogicalDriveStrings(512, buf);
   while (*pbuf != NULL){
      CreateShortcut(pbuf);
      pbuf += lstrlen(pbuf) + 1;
   }
   
   return 0;
}

HRESULT DiskShortcuts::CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc){
   HRESULT hres;
   IShellLink* psl;

   // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
   // has already been called.
   hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
   if (SUCCEEDED(hres))
   {
      IPersistFile* ppf;

      // Set the path to the shortcut target and add the description.
      psl->SetPath(lpszPathObj);
      psl->SetDescription(lpszDesc);

      // Query IShellLink for the IPersistFile interface, used for saving the
      // shortcut in persistent storage.
      hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);

      if (SUCCEEDED(hres))
      {
         WCHAR wsz[MAX_PATH];

         // Ensure that the string is Unicode.
         MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);

         // Add code here to check return value from MultiByteWideChar
         // for success.

         // Save the link by calling IPersistFile::Save.
         hres = ppf->Save(wsz, TRUE);
         ppf->Release();
      }
      psl->Release();
   }
   return hres;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites