Thread: Few questions: use of a manifest, string modification

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Few questions: use of a manifest, string modification

    Hello everyone,

    This is (as far as I can recall) my first post here, so "Hello", and if I do something worthy of criticism, please go easy. :-p

    I have two questions.

    First, I'm trying to make a program that needs to be run as administrator. I've seen several programs, one example is MalwareBytes anti-malware, which has the microsoft shield icon added to the program icon, indicating that the program will be run as an administrator. I need to do something like that with my program, so that users do not need to right click and choose "Run As Administrator", and can instead just run it, and get the "Are you sure" (or whatever) prompt. From what I've read, the way to do this is using a manifest. The thing is, I haven't been able to find any information on how to attach a manifest to my program. I don't know if it makes a difference, but I'm using Dev-C++. I'm assuming that I'd make a resource file that somehow references the manifest, but I really have no idea.

    Second, I need help with string modification. I need to copy the program that I am running, using the program that I'm running to the appropriate location. I tried using
    Code:
    CopyFile(GetCommandLine(), "C:\\whatever\\directory\\is\\destination\\filename.exe", TRUE)
    , however, the issue is that GetCommandLine() returns the directory without the double back-slashed to compensate for the escape character, so CopyFile fails. What I need to do then, is find a way of adding the second backslash to the value that is returned from GetCommandLine().

    This actually leads me to a sub-question which is not as important as the others, but here goes:
    I tried using strtok() to split up the string that contained the directory and file name, at the backslashes. My plan was to split it up into all of the seperate parts and then to use strcat(? Think that's the one, either way to add strings), to put the strings back together with the double backslashes. Now this was just a test on a seperate random source file and is not the program I'm actually writing, but anyway, here's what I tried (which failed):
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    FILE *fp;
    int writefile(FILE *fname, char *wstring);
    
    int main(int argc, char *argv[]){
        
        char *original = GetCommandLine();
        writefile(fp, original);
        char *tok[17];
        *tok[0] = strtok(original, "\\");
        int i = 1;
        while(*tok[i] != NULL){
                  printf("%s\n", tok[i]);
                  *tok[i] = strtok(NULL, "\\");
                  i++;
        }
        //printf("%s\n", original);
        
        system("pause");
        
        return 0;
    }
    
    int writefile(FILE *fname, char *wstring){
        fp = fopen("tester.txt", "w");
        if(fp == NULL)
              printf("Error\n");
        fprintf(fp, "%s\n", wstring);
        fclose(fp);
    }
    What I was trying to do was make an array of pointers which would be strings returned by strtok(). For some reason this kept giving me compiler errors. Specifically this error:

    C:\Documents and Settings\Owner\My Documents\Dev-C++\randomtemp.cpp invalid conversion from `char*' to `char'

    I don't know if that matters, because I'm thinking there must be an easier way to do this which I hope someone here can help me with .

    Anyways, that's all of my stupidity concentrated into one post, so if anyone can help me with this it would be super-amazingly appreciated. (Seriously)

    Thanks in advance,
    -Primux (slackwarefan)

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slackwarefan View Post
    First, I'm trying to make a program that needs to be run as administrator. I've seen several programs, one example is MalwareBytes anti-malware, which has the microsoft shield icon added to the program icon, indicating that the program will be run as an administrator. I need to do something like that with my program, so that users do not need to right click and choose "Run As Administrator", and can instead just run it, and get the "Are you sure" (or whatever) prompt. From what I've read, the way to do this is using a manifest. The thing is, I haven't been able to find any information on how to attach a manifest to my program. I don't know if it makes a difference, but I'm using Dev-C++. I'm assuming that I'd make a resource file that somehow references the manifest, but I really have no idea.
    Splitting this into two answers. . . and oh, by the way -- OUCH!

    Okay, first I'd guess that MalwareBytes is actually running as a service and not as a program. IIRC, administrator MUST be the one that installs this app, thereby allowing unprivileged users the opportunity to access it without needing Admin rights.

    This, however, is mainly a GUESS.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Several issues with your code ( that I can spot without compiling it):

    1) Where are you allocating memory for the "array" of strings?

    From what I can tell you want a an array of strings that are all size 17, however there is no such memory allocation.

    2) You are testing if tok[i] is NULL for i starting at 1. It won't be NULL, it will always be some garbage because tok[i] for i >= 1 is NEVER INITIALIZED. Perhaps you should printf(tok[i]) after calling strtok.

    3) For God's sakes don't use DevCpp. It's garbage from my point of view. Another free alternative would be CodeBlocks which is usually highly recommended on this forum and is IMO 10 times better than Dev.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slackwarefan View Post
    Second, I need help with string modification. I need to copy the program that I am running, using the program that I'm running to the appropriate location. I tried using
    Code:
    CopyFile(GetCommandLine(), "C:\\whatever\\directory\\is\\destination\\filename.exe", TRUE)
    , however, the issue is that GetCommandLine() returns the directory without the double back-slashed to compensate for the escape character, so CopyFile fails. What I need to do then, is find a way of adding the second backslash to the value that is returned from GetCommandLine().
    Do who by what? You do understand that when you build this string:

    char astring[] = "This \\ has \\ backslashes \\ \\ \\ \\ in it.\n";

    that the string actually is:
    "This \ has \ backslashes \ \ \ \ in it
    "

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    You do understand that when you build this string:

    char astring[] = "This \\ has \\ backslashes \\ \\ \\ \\ in it.\n";

    that the string actually is:
    "This \ has \ backslashes \ \ \ \ in it
    Yes, I understand that, that's why I need to add the backslashes.

    From what I can tell you want a an array of strings that are all size 17, however there is no such memory allocation.
    No I'm trying to make an array with 17 elements which are pointers to strings. If that's the best way to do this, then how do I do this, otherwise, what's the best way to add these backslashes?

    -Thanks

    P.S. Kennedy: why do you say:
    and oh, by the way -- OUCH!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by slackwarefan View Post
    Yes, I understand that, that's why I need to add the backslashes.
    If you understood that, then you would know why you don't need to add the backslashes. The double backslashes only appear in your code, never in the actual data itself. If you pass CopyFile a path with two consecutive backslashes in it, CopyFile will be not amused.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use forward slashes?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    If you understood that, then you would know why you don't need to add the backslashes. The double backslashes only appear in your code, never in the actual data itself. If you pass CopyFile a path with two consecutive backslashes in it, CopyFile will be not amused.
    I understand that, however I got an error when I passed single backslashes. I'm not expecting it to show double backslashes as part of the path, but if the string only has a single backslash then copyfile will think it's an escape character. Nonetheless, I got that issue resolved.

    The only remaining issue I have is how do I attach a manifest to my project?

    Anyone familiar with this?

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by slackwarefan View Post
    P.S. Kennedy: why do you say:
    Reading the whole thing at one time hurt my head. . . when I hurt I say OUCH!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM