CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33
  1. #16
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Thanks again, now i tried to put your code into my and everything seems to be fine expect few little errors which i can't make right =(.

    Here is my whole code:

    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    #pragma comment(lib, "Advapi32.lib.")
    
    using namespace std;
    
    
    int main ()
    {
    	BOOL bGetBinary = FALSE;
    	HKEY hKey = NULL;
    	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), NULL, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
    	{
    		DWORD dwType = 0, dwBufferSize = 0;
    		if (!bGetBinary)
    		{
    			dwBufferSize = 260;
    			TCHAR tData[260];
    			if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&tData, &dwBufferSize) == ERROR_SUCCESS)
    			{
    				MessageBox(tData);
    			}
    			else
    				MessageBox(TEXT("Error!"));
    		}
    		else
    		{
    			dwBufferSize = 1024;
    			BYTE bBuffer[1024];
    			if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&bBuffer, &dwBufferSize) == ERROR_SUCCESS)
    			{
    				
    			}
    			else
    				MessageBox(TEXT("Error!"));
    
    		}
    
    	}

    I got errors here:

    Code:
    MessageBox(tData);
    1 IntelliSense: argument of type "TCHAR *" is incompatible with parameter of type "HWND"


    Code:
    MessageBox(TEXT("Error!"));
    3 IntelliSense: argument of type "const wchar_t *" is incompatible with parameter of type "HWND"

    Code:
    MessageBox(TEXT("Error!"));
    5 IntelliSense: argument of type "const wchar_t *" is incompatible with parameter of type "HWND"


    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    I got following error for all of this lines:
    2 IntelliSense: too few arguments in function call



    Also 1 more question, after program opens registry value how can I make the program write it's value on the screen? I tried cin << ProductName; but it won't work.

    PS. program worked after i erased those messagebox, as it was erased so were the errors.

    Code:
    dwBufferSize = 1024;
    			BYTE bBuffer[1024];
    			if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&bBuffer, &dwBufferSize) == ERROR_SUCCESS)
    			{
    				cout << ProductName;
    			}


    I'm really sorry for being such a pain in the *** but I really don't understand what went wrong now. =(

    Any conclusion?


    Thanks
    Last edited by harkoslav; July 22nd, 2010 at 10:24 AM.

  2. #17
    Join Date
    Jul 2010
    Posts
    11

  3. #18
    Join Date
    Jul 2010
    Location
    Mexico
    Posts
    21

    Re: Working with registry

    Sorry, i forgot the HWND parameter, i wrote that code on an MFC app so it just needed the string parameter (as Dadidum said).

    To get the code working just change:

    Code:
    MessageBox(tData);
    MessageBox(TEXT("Error!"));
    to

    Code:
    MessageBox(NULL, tData);
    MessageBox(NULL, TEXT("Error!"));
    The NULL instead of a window handle (HWND-type parameter, a long-type value) is because MessageBox requires an owner window, and since you don't have any, you can pass a NULL value. Modify the other MessageBox'es and you're done.

    If you want to display the value on the screen by using std::cout then you should use the buffer where the values are stored, in this case, the tData variable of the first RegQueryValue:

    Code:
    dwBufferSize = 260;
    TCHAR tData[260];
    if (RegQueryValueEx(hKey, TEXT("ProductName"), NULL, &dwType, (LPBYTE)&tData, &dwBufferSize) == ERROR_SUCCESS)
    {
    	wcout << tData;
    }
    else
    	wcout << TEXT("Error!");
    The second RegQueryValue gets the raw binary data from the registry in a BYTE array (BYTE equals to unsigned char for windows) and i only included that because you said you wanted to get some binary data, but the info we're getting from the registry in this example is a REG_SZ so the first options is a better choice since it gets the string directly. Play with the code, and again, check the msdn docs (you may use your VS help or try at www.msdn.com) because windows uses a lot of strange data types that you should know about, if you have more questions, please let us know.

    And another advice: i used wcout instead of cout because your'e using Unicode, the 'w' before some functions means that it is the 'wide-char' version of the function. Check this post for more advice on cout and wcout http://www.codeguru.com/forum/showthread.php?t=500451

    And don't worry, this forum was made to allow us to share our knowledge with others, sometimes we need a little help, sometimes we can give a little help :-)
    Last edited by bioHzrdmX; July 22nd, 2010 at 09:59 AM.
    "A program is never less than 90% complete, and never more than 95% complete."

  4. #19
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Thanks alot mate, you really helped me!!!
    I made it work finally. Btw I tried to send you an private message but you got disabled private messaging. =(

    Only thing that confuses me is that "unicode". I guess I'm too new to c++ to understand it right now atm, so im going to study more basics


    My FINAL questiond is; xD

    Is it possible to send that value (tData) to my website?
    Last edited by harkoslav; July 22nd, 2010 at 11:02 AM.

  5. #20
    Join Date
    Jul 2010
    Location
    Mexico
    Posts
    21

    Re: Working with registry

    Hi, sorry about the PM thing, i just forgot to turn it on :-)

    Unicode may be a bit confussing for some people (it was for me) but once you learn how to interact with it, you'll be ready to work with Unicode and non-Unicode apps.

    It is not more than a type-change: instead of char you'll be using WCHAR, you'll need some new functions (strcpy, printf, etc. will not longer work for you) that are included with the VC++ compiler, the documentation is your best friend. I will also suggest you to read a bit on Windows data types, to learn what is an HWND, an LPTSTR, a DWORD, etc. If you're gonna programming for windows, this is a must. Try to play with API, play a sound, show a message box, create a window, and you'll be above the rest of the coders in a couple of months. I'm a third-year university student and none of my classmates know how to create a window! Imagine the great work opportunities they'll miss!

    About your final question: yep, it is possible, the easiest way (i think) is using an API call to launch the browser (ShellExecute) with some parameters on the address (you know something like 'http://www.mysite.com/receive.php?data=something' where 'something' is the value of tData 'printed-out' to the string) but this may be a bit tricky and you will need to write the 'receive.php' code to store or show the value on the web page (requirements: php knowledge and php support on your host) so i think you should try to get better programming skills before trying that :-)

    So if you want to become a great coder, you must make a little research, but before trying to run tutorial code, you must learn the essentials about windows, its API and improve your C++ skills, because once you've learned API you will want to make the move to MFC.

    Keep the hard work!!
    Regards.
    Last edited by bioHzrdmX; July 22nd, 2010 at 11:16 AM.
    "A program is never less than 90% complete, and never more than 95% complete."

  6. #21
    Join Date
    Apr 2010
    Posts
    172

    Re: Working with registry

    Just to give you help RegOpenKeyEx does not work in XP

  7. #22
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Working with registry

    Quote Originally Posted by gaar321 View Post
    Just to give you help RegOpenKeyEx does not work in XP
    Are you sure?
    Victor Nijegorodov

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Working with registry

    Lots of old timers prefer the use the brute force method of reading and writing the registry. Me, I'm always looking for the easier approach, yet one that still gets the work done in a reliable and robust manner.

    So consider using the ATL CRegKey class. All you need to do for this code to work is #include <atlbase.h>. No other libs or dlls are required.

    Here's how easy it is...
    Code:
    CString sProductName;
    ULONG ulChars = MAX_PATH + 1;
    
    CRegKey rk;
    rk.Open( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), KEY_READ );
    rk.QueryStringValue( _T("ProductName"), sProductName.GetBuffer( MAX_PATH ), &ulChars );
    
    sProductName.ReleaseBuffer( );
    In terms of reading the registry, a good general rule is to only read the registry if the data isn't available via an api.

    You can retrieve the ProductName from the GetVersionEx api. No need to read the registry at all for this info. In fact, depending on the OS, the ProductName value may not be present.

  9. #24
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Thanks for making me know that but could you please explain me how can I make this work:

    #include <atlbase.h>

    I put that in my code and i got error that it could not get opened.

  10. #25
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Working with registry

    Quote Originally Posted by harkoslav View Post
    Thanks for making me know that but could you please explain me how can I make this work:

    #include <atlbase.h>

    I put that in my code and i got error that it could not get opened.
    What version of Visual C++ are you using?

  11. #26
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Visual Studio C++ 2010

  12. #27
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Working with registry

    Quote Originally Posted by harkoslav View Post
    Visual Studio C++ 2010
    What edition?
    Victor Nijegorodov

  13. #28
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Microsoft Visual Studio C++ 2010 Express
    Version 10.0.30319.1 RTMRel

  14. #29
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Working with registry

    AFIK C++ 2010 Express doesn't contain ATL/MFC.
    Victor Nijegorodov

  15. #30
    Join Date
    Jul 2010
    Posts
    45

    Re: Working with registry

    Is there a way to upgrade it or something?
    Last edited by harkoslav; July 23rd, 2010 at 12:13 PM.

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured