?
Solved

Total New Guy

Posted on 2004-11-15
8
Medium Priority
?
353 Views
Last Modified: 2011-09-20
Our company has Visaul C++, we hack our way through.  I would be the closest thing to a programmer here, I'm more of a Access/SQL or web guy than a real programmer.  Eitherway.

I need to write a program in C++, probally a Console application I guess from reading that will look up  a couple values(path to files) in the registry, create subdirectory there, copy file there, then run seperate program.  

This doesn't have to be flashy or anything, just functional.

Help with the code please. Quicker the better

Jay
0
Comment
Question by:CrossWired
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12585440
To look up values in the registry, you'll have to use the APIs that are made for that, e.g. like

    HKEY hKey = NULL;
    char szDesiredKey[MAX_PATH];
    char szDesiredValue[MAX_PATH];
    long lResult = ERROR_SUCCESS;
    DWORD dwType = REG_SZ;
    DWORD dwSize = MAX_PATH;

    strcpy( szDesiredKey, "Software\\Company\\Application" );

    lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szDesiredKey, 0, KEY_QUERY_VALUE, &hKey );

    if( ERROR_SUCCESS != lResult )
    {
        return( FALSE );
    }

    lResult = RegQueryValueEx( hKey, "SomeValue", NULL, &dwType, ( LPBYTE )szDesiredValue, &dwSize );

    if( ERROR_SUCCESS != lResult )
    {
        return( FALSE );
    }

   RegCloseKey( hKey );


To create a subdirectory, you can use 'CreateDirectory()', to start a program, 'ShellExecute()' will come in handy. Or, in a nutshell:

#include <windows.h>
#include <stdio.h>

int main () {

    HKEY hKey = NULL;
    char szDesiredKey[MAX_PATH];
    char szDesiredValue[MAX_PATH];
    long lResult = ERROR_SUCCESS;
    DWORD dwType = REG_SZ;
    DWORD dwSize = MAX_PATH;

    // read subdirectory to create from the registry
    strcpy( szDesiredKey, "Software\\Company\\Application" );

    lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szDesiredKey, 0, KEY_QUERY_VALUE, &hKey );

    if( ERROR_SUCCESS != lResult )
    {
        return( -1 );
    }

    lResult = RegQueryValueEx( hKey, "NewSubdir", NULL, &dwType, ( LPBYTE )szDesiredValue, &dwSize );

    if( ERROR_SUCCESS != lResult )
    {
        return( -2 );
    }

    RegCloseKey( hKey );

    printf("Creating Subdirectory %s\n", szDesiredValue);
    CreateDirectory ( szDesiredValue);

    // launch external program
    ShellExecute(NULL, "open", "notepad.exe", NULL, NULL, SW_SHOW);

    return 0;
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 12585456
For accessing the registry you should have a look at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/registry_functions.asp
Functions to use are
RegOpenKey(Ex)
RegGetValue
RegCloseKey

For simple file operations you have the choice of using the system command:
system ("cd yourPath");
system ("mkdir newdir");

0
 
LVL 4

Author Comment

by:CrossWired
ID: 12585508
jkr:

Got this:
\mdlx.cpp(40) : fatal error C1010: unexpected end of file while looking for precompiled header directive
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 12585534
You need to change the options of your project either to not used precompiled headers or add the files stdafx.h and stdafx.cpp to your project and use stdafx.h as precompiled header file.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12585541
>> C1010: unexpected end of file while looking for precompiled header directive

Sorry, that's one of the problems with VC - create an empty file named 'StdAfx.h' and place

#include "StdAfx.h"

as the 1st line in the source file. VC uses that for these so called 'precompiled headers'
0
 
LVL 13

Expert Comment

by:SteH
ID: 12585548
For the header you can choose in the project options to create it, use a specified one or automatically use a specified file.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12585744
With your skills, I'd go for JScript for this kind of task (i.e. using the Windows Scriping Host).

e.g. reading the registry

http://msdn.microsoft.com/library/en-us/script56/html/wsmthregread.asp

Your web-head should feel at home with the JavaScriptishness of it.
0
 
LVL 4

Author Comment

by:CrossWired
ID: 12585779
Actually this is coming together quite nice, thanks for the help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

749 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