実際にinternet shortcutsを作成するための公式プログラミングインターフェイスがあります。残念ながら、.NETプログラムではインターフェイスをすぐに利用できず、Tlbimp.exe (Type Library Importer)で使用するタイプライブラリもありません。それには2つのオプションしかありません:C#でインターフェイス定義を複製しようとしているか、ネイティブ実装を使用しています。
私はすぐに.NETコードによってを消費することができ、特に、C++/CLI クラスライブラリ、ネイティブ実装と一緒に行く提案します。実装は、ネイティブ実装クラス(InternetShortcutImpl
)と、管理クラスに機能を公開するためのプロキシ(refクラス(InternetShortcut
))の2種類から構成されています。両方のクラスを同じファイルに配置することができます。
この
は、ネイティブアクセスクラスの実装です:
#pragma once
#include <comip.h>
#include <comdef.h>
#include <IntShCut.h>
#include <ShObjIdl.h>
#include <string>
using namespace System::Runtime::InteropServices;
_COM_SMARTPTR_TYPEDEF(IUniformResourceLocatorW, __uuidof(IUniformResourceLocatorW));
_COM_SMARTPTR_TYPEDEF(IPersistFile, __uuidof(IPersistFile));
_COM_SMARTPTR_TYPEDEF(IShellLinkW, __uuidof(IShellLinkW));
class InternetShortcutImpl
{
public:
InternetShortcutImpl()
{
HRESULT hr = ::CoCreateInstance(CLSID_InternetShortcut, nullptr,
CLSCTX_INPROC_SERVER,
IID_IUniformResourceLocatorW,
(void**)&m_spURL);
if (FAILED(hr)) {
throw gcnew ExternalException(nullptr, hr);
}
}
void SetURL(const std::wstring& url) {
HRESULT hr = m_spURL->SetURL(url.c_str(), 0x0);
if (FAILED(hr)) {
throw gcnew ExternalException(nullptr, hr);
}
}
void SetIconLocation(const std::wstring& iconpath, int index) {
IShellLinkWPtr spLink;
HRESULT hr = m_spURL->QueryInterface(IID_PPV_ARGS(&spLink));
if (SUCCEEDED(hr)) {
hr = spLink->SetIconLocation(iconpath.c_str(), index);
}
if (FAILED(hr)) {
throw gcnew ExternalException(nullptr, hr);
}
}
void Save(const std::wstring& pathname, bool remember) {
IPersistFilePtr spFile;
HRESULT hr = m_spURL->QueryInterface(IID_PPV_ARGS(&spFile));
if SUCCEEDED(hr) {
hr = spFile->Save(pathname.c_str(), remember ? TRUE : FALSE);
}
if (FAILED(hr)) {
throw gcnew ExternalException(nullptr, hr);
}
}
private:
IUniformResourceLocatorWPtr m_spURL;
};
そして、これは、本質的にのみ転送し、それがメンバーとして含まれているネイティブクラスに.NETから呼び出すプロキシクラス、次のとおりです。
#include <msclr\marshal_cppstd.h>
using namespace System;
namespace ShortcutInteropLib {
#pragma warning(push)
#pragma warning(disable : 4461) // "this class has a finalizer but no destructor"
public ref class InternetShortcut
{
public:
InternetShortcut() {
m_pImpl = new InternetShortcutImpl();
}
void SetURL(String^ url) {
m_pImpl->SetURL(msclr::interop::marshal_as<std::wstring>(url));
}
void SetIconLocation(String^ iconpath, int index) {
m_pImpl->SetIconLocation(msclr::interop::marshal_as<std::wstring>(iconpath),
index);
}
void Save(String^ pathname, bool remember) {
m_pImpl->Save(msclr::interop::marshal_as<std::wstring>(pathname),
remember);
}
!InternetShortcut() {
delete m_pImpl;
}
private:
InternetShortcutImpl* m_pImpl;
};
#pragma warning(pop)
}
これは、.NETアセンブリから参照できるライブラリ全体を形成します。呼び出し元のコードは自明簡単です:
class Program
{
[STAThread]
static void Main(string[] args)
{
var shortcut = new InternetShortcut();
shortcut.SetURL(@"http://www.example.com");
//shortcut.SetIconLocation(@"http://www.example.com/favicon", 1);
shortcut.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ @"\MyShortCut.url", false);
}
}
これは、デスクトップ上のhttp://www.example.comへのショートカットを作成します。 SetIconLocation
を実装している間は、URLでどのように使用するかわかりません。アイコンインデックスは、結果のMyShortCut.urlファイルに適切に格納されていますが、パスはローカルファイルシステムへのパスとして解釈されます。
コードは私のシステム上で次のインターネットショートカットを作成する:
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
IDList=
URL=http://www.example.com/
C++/CLIクラスライブラリは、ネイティブコードが含まれているので、あなたが選択する必要がありますターゲットアーキテクチャ。クライアントコードは適切なアセンブリを参照する必要があります。
私はURLを渡すことができます特別な構文があるかどうか分からない、とシステムがローカルファイルシステムのパスとして引数を解釈する必要はありません。もし誰かが知っていれば、私はそれについて聞いてうれしいです。
これが発生する原因となるコードではない可能性があります。それはあなたのコンピュータ(すなわちインターネット)上の特定のスレッドまたは機能にアクセスするためのアプリケーションを禁止するWindowsセキュリティかもしれません – jdave
したがって、私は "Prop4 = blah blah、Prop2 = blah blah"のものを再作成しようとすべきではありませんか? 私はそれらが何であるか、それらにアクセス/作成する方法がわかりません。 – Hanny
ピン設定のショートカットを右クリックしようとしましたが、アクセスプロパティが表示されましたか?多分管理者が必要でしょうか? – jdave