CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2001
    Location
    USA
    Posts
    8

    Help with SetCurrentDirectory in NT

    Hello everyone,

    I posted this message some time ago, but got no response...
    I really need help on this one, if anybody has
    anything to add or information that might help. I'd really appreciate it.

    Here's a good one for you bust your development mind on.
    I have an application (we'll call this "A") that is creating an instance of another application
    ("B") through a COM call. My "A" code creates an Object "B" and then is able
    to control it correctly using functions and methods exposed by "B".
    Program "B" is a bit picky when it is created or destroyed.
    It positively has to have the Windows current directory set to its installed directory (we'll call
    this "C:\Program Files\B_Prog\") in order for the application to start correctly.
    Here is the code I use to do this:


    ' in the Declarations of the Main Module
    public Declare Function GetWindowsDirectory Lib "kernel32" _
    Alias "GetWindowsDirectoryA" (byval lpBuffer as string, _
    byval nSize as Long) as Long
    public Declare Function SetCurrentDirectory Lib "kernel32" _
    Alias "SetCurrentDirectoryA" (byval lpPathName as string) as Long

    Global BApp as B.Application

    ' In the Main Section
    If FindThisHandle(B_Title) = 0 then
    ERRLINE = ERRLINE & Chr$(13) & "Previous B not found"
    If Dir(DirStringToSet, vbDirectory) <> "" then
    ' need to temporaryily set the working directory to the B location for the startup and drivers to load.
    ERRLINE = ERRLINE & Chr$(13) & "Correct Directory for working found"
    If SetCurrentDirectory("C:\Program Files\B_Prog\") <> 0 then
    ERRLINE = ERRLINE & Chr$(13) & "Working Directory set..."
    set B_App = CreateObject("B.Application")
    ERRLINE = ERRLINE & Chr$(13) & "B OBJECT CREATED"
    While BApp.IsInitialized = false
    doomtri = doomtri + 1
    If doomtri < 19000 then
    ' Life is good... it should be done.
    else
    Mess = "Couldn't properly initialize B Program, please start and initialize B first, exit out, and then try to start Program A."

    Mess = Mess & Chr$(13) & "A computer re-boot might also necessary. Program A will now exit."
    MsgBox Mess, vbCritical
    End

    End If
    Wend
    ERRLINE = ERRLINE & Chr$(13) & "Initilzation has taken place... " & doomtri & " Cycles made"
    IsBAppStarted = true
    dBWin = FindThisHandle(B_Title)
    ERRLINE = ERRLINE & Chr$(13) & "set the B indicator tp true."

    else

    IsBAppStarted = false
    ERRLINE = ERRLINE & Chr$(13) & "set the B Indicator to false"
    MsgBox "Couldn't Start Program B. Program A will now shutdown.", vbCritical
    End
    End If






    In the Windows 95 or 98(se) environment, the SetCurrentDirectory does its job
    and set the windows directory to the right spot allowing me to make a successful
    call to the B Program object.
    The same deal happens when the time comes to terminate the object.
    My colleges and I discovered something disturbingly consistent
    in Windows NT and 2000 though.
    Through a few code tests, we were able to find that SetCurrentDirectory
    will not set the windows
    directory desired for an object invoked through COM in the way we
    are doing it now. This was confirmed by creating a small COM program to
    display "HELLO World!" and
    setting the directory beforehand. The GetCurrentDirectory function was
    successful but returned "C:\Winnt"... the windows path directory.
    My question is:
    Has anyone else had a similar problem with these functions when using
    COM in the Windows NT or 2000 platform? I suspect it might be a very common problem as we have tried three different
    machines and the same deal occurs.

    Thanks for any help in advance...



    Ron D.
    This request or reply is sponsered by:
    Coffee -- the breakfast of champions!

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Help with SetCurrentDirectory in NT

    May be this can apply to your situation:
    From Api-guide:
    "The SetCurrentDirectory function changes the current directory for the current process."
    So, if your Com application runs in a different process due to winnt/win2000, it will not be affected. May be you should repeat the setCurrentDirectory from within the com component.

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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