Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetOpenFileName in a service

GetOpenFileName in a service

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheGreatAndPowerfulOz
    wrote on last edited by
    #1

    I am attempting to use GetOpenFileName from a service on W2K and WXP. The service is set to interact with the desktop. The problem is that when the open dialog appears and the user selects the "Desktop" icon on the left panel of the open file dialog, it complains about "C:\Documents and Settings\LocalSystem\desktop" not existing. Note the "LocalDesktop" in that string. I want it to be the name of the current user. So, how do I tell GetOpenFileName to use the currently logged-on user's context when using the open file dialog? Do I need to impersonate the logged-on user before calling GetOpenFileName? If so, how do I do impersonation WITHOUT asking for user credentials? Some rudimentary example will suffice, you don't have to right the whole she-bang. Thank you in advance. ahz

    T 1 Reply Last reply
    0
    • T TheGreatAndPowerfulOz

      I am attempting to use GetOpenFileName from a service on W2K and WXP. The service is set to interact with the desktop. The problem is that when the open dialog appears and the user selects the "Desktop" icon on the left panel of the open file dialog, it complains about "C:\Documents and Settings\LocalSystem\desktop" not existing. Note the "LocalDesktop" in that string. I want it to be the name of the current user. So, how do I tell GetOpenFileName to use the currently logged-on user's context when using the open file dialog? Do I need to impersonate the logged-on user before calling GetOpenFileName? If so, how do I do impersonation WITHOUT asking for user credentials? Some rudimentary example will suffice, you don't have to right the whole she-bang. Thank you in advance. ahz

      T Offline
      T Offline
      TheGreatAndPowerfulOz
      wrote on last edited by
      #2

      After some research, I answered my own question: OPENFILENAME op; ZeroMemory(&op, sizeof(op)); op.lStructSize = sizeof(op); op.hwndOwner = m_hWnd; op.lpstrFilter = L"JavaScript Files (*.js)\0*.js\0VBScript Files (*.vbs)\0*.vbs\0All Files (*.*)\0*.*\0\0\0"; op.lpstrFile = m_wsFilePathBuf; op.nMaxFile = cPathSize; op.lpstrFileTitle = m_wsFileNameBuf; op.nMaxFileTitle = cNameSize; op.lpstrTitle = title.Length() ? title : L"Product Name Title"; op.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY; bool impersonated = false; DWORD processID; if (ProcessIsRunning(L"someuserprocess.exe", processID)) { HANDLE processHandle; HANDLE token = 0, duptoken = 0; BOOL result; DWORD size = 0; processHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE,processID); result = ::OpenProcessToken(processHandle, TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &token); result = result ? ::DuplicateToken(token, SecurityImpersonation, &duptoken) : FALSE; impersonated = (result ? ::ImpersonateLoggedOnUser(duptoken) : FALSE) == TRUE; } BOOL result = GetOpenFileName(&op); if (impersonated) { RevertToSelf(); impersonated = false; }

      X R 2 Replies Last reply
      0
      • T TheGreatAndPowerfulOz

        After some research, I answered my own question: OPENFILENAME op; ZeroMemory(&op, sizeof(op)); op.lStructSize = sizeof(op); op.hwndOwner = m_hWnd; op.lpstrFilter = L"JavaScript Files (*.js)\0*.js\0VBScript Files (*.vbs)\0*.vbs\0All Files (*.*)\0*.*\0\0\0"; op.lpstrFile = m_wsFilePathBuf; op.nMaxFile = cPathSize; op.lpstrFileTitle = m_wsFileNameBuf; op.nMaxFileTitle = cNameSize; op.lpstrTitle = title.Length() ? title : L"Product Name Title"; op.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY; bool impersonated = false; DWORD processID; if (ProcessIsRunning(L"someuserprocess.exe", processID)) { HANDLE processHandle; HANDLE token = 0, duptoken = 0; BOOL result; DWORD size = 0; processHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE,processID); result = ::OpenProcessToken(processHandle, TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &token); result = result ? ::DuplicateToken(token, SecurityImpersonation, &duptoken) : FALSE; impersonated = (result ? ::ImpersonateLoggedOnUser(duptoken) : FALSE) == TRUE; } BOOL result = GetOpenFileName(&op); if (impersonated) { RevertToSelf(); impersonated = false; }

        X Offline
        X Offline
        xxrl
        wrote on last edited by
        #3

        Thanks for your sharing... I studied more by this issue. thanks very much ,i will remember this. but do you have msn acount? You are the best!Me too!

        1 Reply Last reply
        0
        • T TheGreatAndPowerfulOz

          After some research, I answered my own question: OPENFILENAME op; ZeroMemory(&op, sizeof(op)); op.lStructSize = sizeof(op); op.hwndOwner = m_hWnd; op.lpstrFilter = L"JavaScript Files (*.js)\0*.js\0VBScript Files (*.vbs)\0*.vbs\0All Files (*.*)\0*.*\0\0\0"; op.lpstrFile = m_wsFilePathBuf; op.nMaxFile = cPathSize; op.lpstrFileTitle = m_wsFileNameBuf; op.nMaxFileTitle = cNameSize; op.lpstrTitle = title.Length() ? title : L"Product Name Title"; op.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY; bool impersonated = false; DWORD processID; if (ProcessIsRunning(L"someuserprocess.exe", processID)) { HANDLE processHandle; HANDLE token = 0, duptoken = 0; BOOL result; DWORD size = 0; processHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE,processID); result = ::OpenProcessToken(processHandle, TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &token); result = result ? ::DuplicateToken(token, SecurityImpersonation, &duptoken) : FALSE; impersonated = (result ? ::ImpersonateLoggedOnUser(duptoken) : FALSE) == TRUE; } BOOL result = GetOpenFileName(&op); if (impersonated) { RevertToSelf(); impersonated = false; }

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          Good solution, just remember to close your handles when you're done. Also, a couple of points...

          • What if there is no user running the process you're checking for?
          • What if more than one user is running the process you're checking for? Do you use the right one?

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          T 1 Reply Last reply
          0
          • R Ryan Binns

            Good solution, just remember to close your handles when you're done. Also, a couple of points...

            • What if there is no user running the process you're checking for?
            • What if more than one user is running the process you're checking for? Do you use the right one?

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #5

            Ryan Binns wrote: Good solution, just remember to close your handles when you're done. yes, caught that and did it. Also, a couple of points... What if there is no user running the process you're checking for? First, in the product I'm developing is an endpoint security product, so if the process queried is not running, the product considers that a fatal error and causes user's machine to reboot. What if more than one user is running the process you're checking for? Do you use the right one? Second the product I'm developing is not supported on FUS or TSERVER or CITRIX metaframe. wouldn't make sense in those environments. Thanks for the comments.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups