要运行的命令行
netsh interface ip set address "本地连接" source=dhcp
在win7中如果直接运行会提示,权限不够,要右键,使用管理员权限运行。
那么我考虑把该命令行放在一个MFC的程序中运行。
我尝试了ShellExecute
HINSTANCE rst=ShellExecute(GetSafeHwnd(),_T("open"),_T("netsh"),_T(" interface ip set address \"本地连接\" source=dhcp"),NULL,SW_MAXIMIZE);
有cmd窗口一闪而过,但是检查本地连接,没成功。也看不到netsh运行的结果到底如何?
请问如何实现,在MFC程序中运行:netsh interface ip set address "本地连接" source=dhcp
如果是用shellExecute或者ShellExecuteEx,里面的参数如何用法?

解决方案 »

  1.   

    一个MFC程序打开chm的例子
    shellExecute(this->m_hWnd,"open","流浪仙人.chm",NULL,NULL,SW_SHOWMAXIMIZED);MSDN解释hwnd
    [in] Handle to the owner window used for displaying a user interface (UI) or error messages. This value can be NULL if the operation is not associated with a window.
    lpOperation
    [in] Pointer to a null-terminated string, referred to in this case as a verb, that specifies the action to be performed. The set of available verbs depends on the particular file or folder. Generally, the actions available from an object's shortcut menu are available verbs. For more information about verbs and their availability, see Object Verbs. See Extending Shortcut Menus for further discussion of shortcut menus. The following verbs are commonly used.
    edit
    Launches an editor and opens the document for editing. If lpFile is not a document file, the function will fail.
    explore
    Explores the folder specified by lpFile.
    find
    Initiates a search starting from the specified directory.
    open
    Opens the file specified by the lpFile parameter. The file can be an executable file, a document file, or a folder.
    print
    Prints the document file specified by lpFile. If lpFile is not a document file, the function will fail.
    NULL
    For systems prior to Microsoft Windows 2000, the default verb is used if it is valid and available in the registry. If not, the "open" verb is used.For Windows 2000 and later systems, the default verb is used if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.lpFile
    [in] Pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. Note that not all verbs are supported on all objects. For example, not all document types support the "print" verb.
    lpParameters
    [in] If the lpFile parameter specifies an executable file, lpParameters is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.
    lpDirectory
    [in] Pointer to a null-terminated string that specifies the default directory.
    nShowCmd
    [in] Flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it.
    SW_HIDE
    Hides the window and activates another window.
    SW_MAXIMIZE
      

  2.   

    ShellExecuteExSHELLEXECUTEINFO sei={sizeof(SHELLEXECUTEINFO)};
    sei.lpVerb=TEXT("runas");
      

  3.   

    用runas verb来提升UAC权限.
    CMD窗口除非你进到命令行模式,不然输出窗口就会一闪而过,需要通过管道等方式获取输出
      

  4.   


    我感觉不仅仅是权限的问题,netsh 这么长的命令行在ShellExecute中执行好像也是有问题的。能否给个ShellExecute执行netsh的例子。
    不知道,你有否试过,runas仍然不行。