SHOpenFolderAndSelectItems Win32 API не работает на Go, но работает на C#C#

Место общения программистов C#
Ответить
Anonymous
 SHOpenFolderAndSelectItems Win32 API не работает на Go, но работает на C#

Сообщение Anonymous »

Я пытаюсь открыть окно проводника Windows с указанными элементами в определенной выбранной папке.
Я пытался реализовать программу, используя SHOpenFolderAndSelectItems Win32 API с C#. Следуя ответу здесь: https://stackoverflow.com/a/12262552

Код: Выделить всё

using System.Runtime.InteropServices;

SelectInFileExplorer("C:\\Users");

void SelectInFileExplorer(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
throw new ArgumentNullException("fullPath");

fullPath = Path.GetFullPath(fullPath);

IntPtr pidlList = NativeMethods.ILCreateFromPathW(fullPath);
if (pidlList != IntPtr.Zero)
try
{
// Open parent folder and select item
Marshal.ThrowExceptionForHR(NativeMethods.SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
}
finally
{
NativeMethods.ILFree(pidlList);
}
}

static class NativeMethods
{

[DllImport("shell32.dll", ExactSpelling = true)]
public static extern void ILFree(IntPtr pidlList);

[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern IntPtr ILCreateFromPathW(string pszPath);

[DllImport("shell32.dll", ExactSpelling = true)]
public static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags);
}
Это сработало, но теперь я хотел бы реализовать то же самое с помощью Go.
Вот мой код:

Код: Выделить всё

package main

import (
"fmt"
"path/filepath"
"syscall"
"unsafe"
)

func main() {
SelectInFileExplorer("C:\\Users")
fmt.Println("Hello, World!")
}

func SelectInFileExplorer(fullPath string) {
if fullPath == "" {
panic("fullPath cannot be empty")
}

fullPath, _ = filepath.Abs(fullPath)

pidlList, err := ILCreateFromPathW(fullPath)
if err != nil {
panic(err)
}
defer ILFree(pidlList)

// Open parent folder and select item
err = SHOpenFolderAndSelectItems(pidlList, 0, 0)
if err != nil {
panic(err)
}
}

func ILFree(pidlList uintptr) {
shell32 := syscall.NewLazyDLL("shell32.dll")
proc := shell32.NewProc("ILFree")
proc.Call(pidlList)
}

func ILCreateFromPathW(pszPath string) (uintptr, error) {
shell32 := syscall.NewLazyDLL("shell32.dll")
proc := shell32.NewProc("ILCreateFromPathW")

pszPathPtr, err := syscall.UTF16PtrFromString(pszPath)
if err != nil {
return 0, err
}

ret, _, err := proc.Call(uintptr(unsafe.Pointer(pszPathPtr)))
if ret == 0 {
return 0, err
}
return ret, nil
}

func SHOpenFolderAndSelectItems(pidlList uintptr, cild uint32, children uintptr) error {
shell32 := syscall.NewLazyDLL("shell32.dll")
proc := shell32.NewProc("SHOpenFolderAndSelectItems")

ret, _, err := proc.Call(pidlList, uintptr(cild), children, 0)
if ret != 0 && err.Error() != "The operation completed successfully." {
return err
}
return nil
}
Это не работает, поскольку после выполнения кода ничего не произошло. Проводник не появился.
Я прочитал некоторые документы, касающиеся использования API: https://learn.microsoft.com/en-us/windo ... hlobj_core /nf-shlobj_core-shopenfolderandselectitems
Все еще не понимаю, что происходит. Возможно, дело не в самом языке программирования. Я думаю, что вызов функций DLL в GO немного сложен, и я, вероятно, сделал что-то не так с вызовами функций.

Подробнее здесь: https://stackoverflow.com/questions/787 ... on-c-sharp
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»