Results 1 to 13 of 13

Thread: GetWindowsDir in C++?

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    GetWindowsDir in C++?

    I haven't used the API in C++ yet; how do you get the Windows System directory in C++?

  2. #2
    jim mcnamara
    Guest
    Code:
    #include <windows.h>
    UINT i;
    char path[MAX_PATH];
    UINT sz = MAX_PATH;
      i = GetWindowsDirectory(path, sz); 
    
      if (i==ERROR_SUCCESS) {
            // you got it --- path is a null terminated string
     }
     else {
            something bombed.... not very likely with this call
            call GetLastError to see what happened
     }

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Thanks.

  4. #4
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    i got a quick q for a similar app that i am doing...

    How do you find where any app is installed. like if i want to know where "somefile.exe" is?

    Is there a standard way of doing it?/

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    #include <windows.h>
    UINT i;
    char path[MAX_PATH];
    UINT sz = MAX_PATH;
      i = GetWindowsDirectory(path, sz); 
    
      if (i==ERROR_SUCCESS) {
            // you got it --- path is a null terminated string
     }
     else {
            something bombed.... not very likely with this call
            call GetLastError to see what happened
     }
    shouldn't it not equal ERROR_SUCCESS if you got it?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by crptcblade
    shouldn't it not equal ERROR_SUCCESS if you got it?
    No

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    PHP Code:
    #include <iostream.h>
    #include <windows.h>

    //using namespace std;

    int main()
    {
        
    UINT i;
        
    char path[MAX_PATH];
        
    UINT sz MAX_PATH;
        
    GetWindowsDirectory(pathsz); 

        if (
    i==ERROR_SUCCESS) {
            
    cout << path << endl;
            
    // you got it --- path is a null terminated string
        
    }
        else {
            
    cout << "error" << endl;
            
    //something bombed.... not very likely with this call
            //call GetLastError to see what happened
        


        return 
    0;


    keeps outputting "error"

    If I say if(i != ERROR_SUCCESS), I get the correct path.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Well the error_success is used in loads of API's to indicate success (as the name suggests)
    If the function succeeds, the return value is ERROR_SUCCESS.

  9. #9

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Has anybody known for the API call to fail?

  10. #10
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    no, but you shouldn't take the risk just because it hasn't on previous occasions (i'm talking generically here)

  11. #11

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    True; so would path just contain a million null chars? So can I assume that if the first char is not null that there was success?

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I thought it returned 0 if it failed, and then you can check GetLastError().
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ahem...

    From MSDN:

    If the function succeeds, the return value is the length, in characters, of the string copied to the buffer, not including the terminating null character.

    If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

    If the function fails, the return value is zero. To get extended error information, callGetLastError.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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