C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 26th, 2012, 05:02 PM
blackDog blackDog is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 blackDog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 27 sec
Reputation Power: 0
General - Building basic String

hi I am working on a app that will trigger a UAC prompt in what ever program is opened by ShellExecute. My problem is I am new to C++ and cant figure out how to hard code a path for the Shell execute to run. As of now this program ShellExecutes what ever path is in arg[0]. How can i Build a string to put in the place of arg[0] on the line,
sinfo.lpFile = arg[0];

Any help would be great! thanks.

p.s. I am very new so if you cant see why making a string to put in that line will solve my problem than you are most likely right. and any advice would good.

Code:
#include "stdafx.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <shellapi.h>
#include <process.h>

#include "uac-example.h"

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst,LPSTR cmdLine, int nCmdShow){
	LPWSTR *arg;
	int argc = 0;
	HRESULT ret = SUCCESS;
	WCHAR imagePath[MAXPATHLEN];
	WCHAR workingDir[MAXPATHLEN];
	WCHAR uacDir[MAXPATHLEN];
	WCHAR uacRunningLockFilePath[MAXPATHLEN];
	HANDLE uacRunningLockFileHandle = INVALID_HANDLE_VALUE;
	WCHAR elevatedLockFilePath[MAXPATHLEN];
	HANDLE elevatedLockFileHandle = INVALID_HANDLE_VALUE;
	arg = CommandLineToArgvW(GetCommandLineW(),&argc);
	//if(arg == NULL || argc < 2) {
	//	ERRORBOX("Missing required program arguments.\n\nUsage:\nuac-example.exe <working directory>");
	//	return FAILURE;
	//}
	GetModuleFileName(NULL, imagePath, MAXPATHLEN);
	arg[0] = imagePath;
	wcscpy_s((wchar_t *)uacDir, MAXPATHLEN, arg[1]);
	_snwprintf_s(uacRunningLockFilePath, MAXPATHLEN, MAXPATHLEN,
					_T("%s/") _T(RUNNING_LOCK_FILE), uacDir);
	wcscpy_s(workingDir, MAXPATHLEN, imagePath);
	WCHAR *slash = wcsrchr(workingDir, '\\');
	wcscpy_s(slash, MAXPATHLEN, _T(""));
	_snwprintf_s(elevatedLockFilePath, MAXPATHLEN, MAXPATHLEN,_T("%s/") _T(ELEVATE_LOCK_FILE), workingDir);
	uacRunningLockFileHandle = CreateFileW(uacRunningLockFilePath,(GENERIC_READ | GENERIC_WRITE),0,NULL,OPEN_ALWAYS,FILE_FLAG_DELETE  _ON_CLOSE,NULL);
	if (uacRunningLockFileHandle == INVALID_HANDLE_VALUE) {
		if (_waccess(elevatedLockFilePath, F_OK) == 0 &&
				_wremove(elevatedLockFilePath) != 0) {
			return FAILURE;
		}
		elevatedLockFileHandle = CreateFileW(elevatedLockFilePath,(GENERIC_READ | GENERIC_WRITE),0,NULL,OPEN_ALWAYS,FILE_FLAG_DELETE  _ON_CLOSE,NULL);
		if(elevatedLockFileHandle == INVALID_HANDLE_VALUE){
			ERRORBOX("Unable to acquire the necessary permissions to run demo app.");
			return FAILURE;
		}
		LPWSTR spawnCmdLine = BuildCommandLine(argc - 1, arg + 1);
		if(!spawnCmdLine){
			CloseHandle(elevatedLockFileHandle);
			ERRORBOX("An error occured while respawning self.");
			return FAILURE;
		}
		SHELLEXECUTEINFO sinfo;
		memset(&sinfo, 0, sizeof(SHELLEXECUTEINFO));
		sinfo.cbSize = sizeof(SHELLEXECUTEINFO);
		sinfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS;
		sinfo.hwnd = NULL;
		sinfo.lpFile = arg[0];
		sinfo.lpParameters = spawnCmdLine;
		sinfo.lpVerb = L"runas"; // <<-- this is what makes a UAC prompt show up
		sinfo.nShow = SW_SHOWMAXIMIZED;
		BOOL result = ShellExecuteEx(&sinfo);
		LocalFree(spawnCmdLine);
		if(result){
			WaitForSingleObject(sinfo.hProcess, INFINITE);
			CloseHandle(sinfo.hProcess);
			return SUCCESS;
		}else{
			return FAILURE;
		}
	}
	EXIT_IF_ELEVATED(elevatedLockFilePath,uacRunningLo  ckFileHandle,SUCCESS);
	LocalFree(arg);
	return SUCCESS;
}

// ----------------------------------------------------------------------
// The following code was taken directly from the Mozilla Firefox Updater
// source tree, and slightly modified to support "Wide" strings in
// Visual C++.
// ----------------------------------------------------------------------

LPWSTR
BuildCommandLine(int argc, LPWSTR *argv){
	int i;
	int len = 0;
	// The + 1 of the last argument handles the
	// allocation for null termination
	for (i = 0; i < argc; ++i) {
		len += ArgStrLen(argv[i]) + 1;
	}
	// Protect against callers that pass 0 arguments
	if (len == 0) {
		len = 1;
	}
	LPWSTR s = (LPWSTR)malloc(len * sizeof(LPWSTR));
	if (!s) {
		return NULL;
	}
	LPWSTR c = s;
	for (i = 0; i < argc; ++i) {
		c = ArgToString(c, argv[i]);
		if (i + 1 != argc) {
			*c = ' ';
			++c;
		}
	}
	*c = '\0';
	return s;
}
int
ArgStrLen(LPWSTR s) {
  int backslashes = 0;
  int i = wcslen(s);
  BOOL hasDoubleQuote = wcschr(s, L'"') != NULL;
  // Only add doublequotes if the string contains a space or a tab
  BOOL addDoubleQuotes = wcspbrk(s, L" \t") != NULL;
  if (addDoubleQuotes) {
    i += 2; // initial and final duoblequote
  }
  if (hasDoubleQuote) {
    while (*s) {
      if (*s == '\\') {
        ++backslashes;
      } else {
        if (*s == '"') {
          // Escape the doublequote and all backslashes preceding the doublequote
          i += backslashes + 1;
        }
        backslashes = 0;
      }

      ++s;
    }
  }

  return i;
}
LPWSTR
ArgToString(LPWSTR d, LPWSTR s) {
  int backslashes = 0;
  BOOL hasDoubleQuote = wcschr(s, L'"') != NULL;
  // Only add doublequotes if the string contains a space or a tab
  BOOL addDoubleQuotes = wcspbrk(s, L" \t") != NULL;
  if (addDoubleQuotes) {
    *d = '"'; // initial doublequote
    ++d;
  }
  if (hasDoubleQuote) {
    int i;
    while (*s) {
      if (*s == '\\') {
        ++backslashes;
      } else {
        if (*s == '"') {
		// Escape the doublequote and all backslashes\
		// preceding the doublequote
          for (i = 0; i <= backslashes; ++i) {
            *d = '\\';
            ++d;
          }
        }
        backslashes = 0;
      }
      *d = *s;
      ++d; ++s;
    }
  } else {
    wcscpy(d, s);
    d += wcslen(s);
  }
  if (addDoubleQuotes) {
    *d = '"'; // final doublequote
    ++d;
  }
  return d;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Building basic String


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap