I want to use the com interface of windows in go, but I encountered an annoying problem. The error message is as follows:
$ gcc main.c -L. -lfileselector -o main
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./fileselector.dll when searching for -lfileselector
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible ./fileselector.dll when searching for -lfileselector
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lfileselector
collect2.exe: error: ld returned 1 exit status
I learned from other people's posts on stackoverflow that this may be a library compiled by vc++, which is not compatible with gcc. My library is indeed compiled with vc++ due to the com interface of windows.
Can CGO of GO language replace the original GCC with VC++ as the compiler?
Is there any way to make VC++ and GCC libraries compatible?
Or, can you call the com interface of windows through the API of MinGW?
Part of the code of the library is as follows:
#include <windows.h>
#include <shobjidl.h>
const char *save_file_dialog() {
const char *filePathString = "";
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (!SUCCEEDED(hr)) return filePathString;
IFileSaveDialog *fileSave;
hr = CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_ALL, IID_IFileSaveDialog, (void **) &fileSave);
if (SUCCEEDED(hr)) {
fileSave->SetFileTypes(sizeof(fileTypes) / sizeof(COMDLG_FILTERSPEC), fileTypes);
fileSave->SetFileTypeIndex(2);
fileSave->Show(nullptr);
IShellItem *item;
hr = fileSave->GetResult(&item);
if (SUCCEEDED(hr)) {
PWSTR filePath;
hr = item->GetDisplayName(SIGDN_FILESYSPATH, &filePath);
if (SUCCEEDED(hr) && filePath) {
filePathString = unicode_to_utf8(filePath);
if (have_suffix(filePathString) == -1) {
UINT fileType{};
fileSave->GetFileTypeIndex(&fileType);
filePathString = (char *) append_suffix(filePathString, fileType);
}
}
item->Release();
}
fileSave->Release();
CoUninitialize();
return filePathString;
} else {
return filePathString;
}
}
Its function is to open a file dialog box through the IFileSaveDialog of the windows com interface.