Thread: directory and windows version?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    directory and windows version?

    Have you any idea how easy it is to get a couple of things form the OS?

    I need the current drive (or drive letter if possible) as some people may not have d:\ as their cd-rom drive

    I also need the windows version, because the objects I have created are platform specific.

    If possible I would like to get these back as variables, and without running a cmd.exe type box to get them.

    I can get the drive by system("cd"), but this is not really what i want. I need just the actual data returned.

    Maybe an API would do it??

    If so how would I implement it?

    cheers,

    dave

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The GetCurrentDirectory() API function will obtain the absolute path, including the drive letter of the current directory. The GetVersionEx() API function will obtain OS version information. Have a look at them in the help, they are both fairly straightforward.

    Post again if you get stuck.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    I have no idea on how to use the API calls - I know this is wrong but it should give an indication of what I am trying to do.

    My application will be on a cd, but I cant be sure that the cd is d:, so I am trying to get the current directory, and return the first 2 characters (i could just return 1) but this should give me c: or d: etc..

    I did it before using a system("cd") command, but it runs a dos window and I want to do it in windows instead.

    the commented out line is the one I will use eventually but the one underneath is the test one I am using.

    CString drive="";
    drive.GetCurrentDirectory();
    drive.Left(2);

    CString wintoc="";

    //wintoc.Format("%s\\cosmos\\instdata\\wintoc.dat",d rive);
    wintoc.Format("%s\\wintoc.dat", drive);
    if(!LoadDataBase(wintoc)) PostQuitMessage(0);

    I don't know how to do this using the API - any pointers.

    cheers,
    dave

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Hmmm, MFC. Still, even MFC programmers can use API routines. MFC is, after all, simply a series of wrappers around API functions. This will get you the current working directory...

    char Path[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, Path);

    ... this should work in an MFC application as well, or at least, I can't see why it should not. All I can say is try it. Note this is a char array not a CString. MAX_PATH is a windows defined constant, you do not need to declare that.

    In MFC there may be another way of doing it, I don't know.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    once again, thankyou for the help adrian, however -

    The GetCurrentDirectory method gets the directory you run the code from.

    At times, it is necessary to run my application from elsewhere, which makes this a problem:

    my script is on my pc - c:\setup.exe

    I am working on another directory - p: (for example)

    If I run c:\setup.exe from my p:\ prompt then the drive it gets is p:, since all of the associated files are stored on c: with the application it will fail to work.

    Is there anything that anyone knows will get the application directory??

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    First...

    >>> The GetCurrentDirectory method

    ... be careful with the terminology here. I know what you mean because we have been talking about it, but as I have just showed you, this is not a method, it is a direct call to a Win32 API function. If you talk to others and say "method" they will almost certainly think you are talking about a method of some class or other.

    Next, don't worry about the details for now, try this and tell me if it gives you what you want.

    HMODULE hMod;
    char *ModulePath[MAX_PATH];
    hMod = GetModuleHandle(NULL);
    GetModuleFileName(hMod, ModulePath, MAX_PATH);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    I got a couple of errors when I compiled it, shown below. Also here is the code.

    Firstly, the new code you gave - the first error refers to this. In the GetModuleFileName should modulePath be '*' ?

    Next is the old code, and then my code for rearranging the string.
    The second error refers to the cast to CString I try to do.

    HMODULE hMod;
    char *ModulePath[MAX_PATH];
    hMod = GetModuleHandle(NULL);
    GetModuleFileName(hMod, ModulePath, MAX_PATH);

    //char Path[MAX_PATH];
    //GetCurrentDirectory(MAX_PATH, Path);
    CString path="";
    path = (CString) ModulePath;
    path = path.Left(2);
    CString wintoc="";

    // Actual line for the cd version below
    //wintoc.Format("%s\\cosmos\\instdata\\wintoc.dat");
    wintoc.Format("%s\\wintoc2.txt", path);
    if(!LoadDataBase(wintoc)) PostQuitMessage(0);
    UpdateListCtrl();

    C:\dave\new\ViewDatabase\ViewDatabaseDlg.cpp(139) : error C2664: 'GetModuleFileNameA' : cannot convert parameter 2 from 'char *[260]' to 'char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\dave\new\ViewDatabase\ViewDatabaseDlg.cpp(144) : error C2440: 'type cast' : cannot convert from 'char *[260]' to 'class CString'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Error executing cl.exe.


    Cheers for the ongoing help.

    dave

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Sorry for delay, board seems really slow for me today!

    First, yes, my goof! That's what you get for typing code without trying it. Of course the declaration should be...

    char ModulePath[MAX_PATH];

    The other, I think you'll find the "=" operator is overloaded for the CString class therefore a cast is not necessary, Ithink , (remember, I don't use MFC), you can just say...

    path = ModulePath;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    BTW, you can simplify this a teeny bit:

    Code:
    HMODULE hMod;
    char ModulePath[MAX_PATH];
    hMod = GetModuleHandle(NULL);
    GetModuleFileName(hMod, ModulePath, MAX_PATH);
    It is not necessary to get the module handle of the calling program (which is what GetModuleHandle(NULL) does) because if you pass a null handle into GetModuleFileName, it automatically does this for the current module.

    So, this code is exactly equivalent:

    Code:
    char ModulePath[MAX_PATH];
    GetModuleFileName(NULL, ModulePath, MAX_PATH);

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> pass a null handle into GetModuleFileName,

    Yes, that's true. I always get spooked when trying to help an MFC'er, so tend to revert to really, really basic calls!

    You're right of course.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windows explorer working directory
    By baccardi in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2009, 08:07 AM
  2. i'm new to linux - which linux to install?
    By wakish in forum Linux Programming
    Replies: 38
    Last Post: 02-27-2006, 12:40 PM
  3. Directory size in Windows
    By mdoland in forum C Programming
    Replies: 3
    Last Post: 11-06-2005, 04:01 PM
  4. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  5. Replies: 2
    Last Post: 09-27-2003, 11:57 AM