2016-12-27 54 views
-1

我試圖創建符號鏈接發生器,C++/CLI(基於Windows) 有微軟(MS符號連接的Windows

一個的Docu但我不知道它excactly是如何工作的。

我tryed找到工作excample,但我無法找到一個和我不能讓我自己一個可行的解決方案。

我的完整代碼HERE!

private: System::Void btnCreateSymlinks_Click(System::Object^ sender, System::EventArgs^ e) { 
    String^ pathSource = GetSourcePath(); 
    String^ pathDest = GetDestinationPath(); 
    String^ folder = System::IO::Path::GetFileName(pathSource); 
    lblInfo->Text = pathSource + " -> " + pathDest + "" + folder; 
} 

如何使用這些pathSource和pathDest的CreateSymbolicLink功能?

+0

此[文章](http://community.bartdesmet.net/blogs/bart/archive/2006/10/24/Windows-Vista-_2D00_-Creating-symbolic-links-with-C_2300_.aspx ) 可能有幫助。 – tinstaafl

+0

它對於C#和我需要C++ – xQp

回答

2

爲了使用垃圾收集堆在.NET字符串,你需要釘住指針(這是一件的P/Invoke的C#程序員自動完成,但C++/CLI程序員需要申請)。

pin_ptr<const wchar_t> wcsTarget = PtrToStringChars(pathSource); 
pin_ptr<const wchar_t> wcsSymlink = PtrToStringChars(Path::Combine(pathDest, folder)); 

然後只需調用Unicode版本的API(因爲.NET字符串總是Unicode)。

CreateSymbolicLinkW(wcsSymlink, wcsTarget, 0U); 
+0

PtrToStringChars不與pin_ptr 至少對我來說我不知道​​ – xQp

+0

@xQp:你有所有的C++/CLI頭已包括? –

+0

我tryed與http://stackoverflow.com/questions/9782669/c-cli-system-string-to-mfc-lpctstr位CreateSymbolicLinkW不工作 – xQp