ShellExecuteA 和::ShellExecuteA 区别
时间: 2024-01-06 13:33:25 浏览: 42
ShellExecuteA 和::ShellExecuteA 是两种不同的函数调用方式。
ShellExecuteA 是 Win32 API 中的一个函数,用于启动一个外部程序或打开一个文件。它需要通过指定一系列参数来执行相应的操作,比如要执行的操作类型、要执行的文件名、命令行参数等。这个函数需要通过调用 Windows 操作系统提供的接口来实现。
而::ShellExecuteA 是使用了全局命名空间限定符(::)来调用 ShellExecuteA 函数。全局命名空间限定符是 C++ 中的一种语法,它可以指定使用全局命名空间中的函数和变量,避免与局部命名空间中的同名函数和变量冲突。在 C++ 代码中使用全局命名空间限定符时,不需要包含特定的头文件,因为 ShellExecuteA 函数已经被 Windows.h 头文件包含。
所以,ShellExecuteA 和::ShellExecuteA 在功能上是完全相同的,只是调用方式略有不同。前者是直接调用 Win32 API 中的函数,后者是通过全局命名空间限定符调用同名函数。
相关问题
panic: Failed to find ShellExecuteA procedure in user32.dll: The specified proce dure could not be found.
如果 `ShellExecuteA` 函数在你的系统上也不可用,那么你可以尝试使用 `CreateProcess` 函数来打开一个浏览器并访问指定的网址。代码如下所示:
```go
package main
import (
"syscall"
"unsafe"
)
func main() {
url := "https://www.example.com"
user32 := syscall.MustLoadDLL("user32.dll")
MessageBox := user32.MustFindProc("MessageBoxW")
kernel32 := syscall.MustLoadDLL("kernel32.dll")
CreateProcess := kernel32.MustFindProc("CreateProcessW")
// 打开一个弹窗
MessageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Message"))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Title"))),
0)
// 打开浏览器并访问网址
var si = new(syscall.StartupInfo)
var pi = new(syscall.ProcessInformation)
err := CreateProcess.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("cmd.exe /c start "+url))),
0,
0,
false,
syscall.CREATE_DEFAULT_ERROR_MODE,
0,
0,
uintptr(unsafe.Pointer(si)),
uintptr(unsafe.Pointer(pi)),
).Error()
if err != nil && err.Error() != "The operation completed successfully." {
panic(err)
}
}
```
这段代码中,我们使用 `CreateProcess` 函数来创建一个新的进程,并在新进程中运行 `cmd.exe` 命令来打开指定的网址。我们使用 `syscall.StartupInfo` 和 `syscall.ProcessInformation` 结构体来指定新进程的属性,例如工作目录和标准输入输出流等。
panic: Failed to find ShellExecuteW procedure in user32.dll: The specified proce dure could not be found.
这个错误可能是因为 `ShellExecuteW` 函数在你的系统上不可用,可能是因为你的系统版本过旧。你可以尝试使用 `ShellExecuteA` 函数代替 `ShellExecuteW` 函数,如下所示:
```go
package main
import (
"syscall"
"unsafe"
)
func main() {
url := "https://www.example.com"
user32 := syscall.MustLoadDLL("user32.dll")
MessageBox := user32.MustFindProc("MessageBoxW")
ShellExecute := user32.MustFindProc("ShellExecuteA")
// 打开一个弹窗
MessageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Message"))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Title"))),
0)
// 打开网址
ShellExecute.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("open"))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(url))),
0,
0,
syscall.SW_SHOW)
}
```
这段代码中,我们将 `ShellExecuteW` 函数替换为 `ShellExecuteA` 函数,并将第三个参数从 `uintptr` 类型的指针转换为 `unsafe.Pointer` 类型的指针。这样可以避免编译器在调用 `ShellExecuteA` 函数时报错。