Tommy 碎碎念

Tommy Wu's blog

« 上一篇 | 下一篇 »

Visual Studio 2012 在 Windows 8 底下無法使用 Shell32.Shell 的問題?
post by tommy @ 19 九月, 2012 17:32

最近開始用 C# 寫 .Net 的程式, 發現就寫程式的方便性來說, 比起直接用 C/C++ 寫要方便許多. 所以... 原本有些為了方便, 使用 php 來寫的 script, 也都順便改用 C# 來寫.

因為 Windows 8 與 Visual Studio 2012 出來後, 就由 MSDN 中抓回來用, 其實算很好用. 不過... 碰到一個原本在 Windows 7 搭配 Visual Studio 2010 時可以正常運作的程式, 改用 Windows 8 搭配 Visual Studio 2012 就會出現錯誤.

出錯的地方很簡單, 就類似這樣:

namespace Shell32Test
{
class Program
{
static void Main(string[] args)
{
object shell = new Shell32.Shell();
}
}
}

會出現下面的錯誤:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

不過, 同一個程式, 在 Windows 7 下, 不管用 Visual Studio 2010 或 2012 都可以運作. 而且, 把那個可以運作的執行檔, 複製到 Windows 8 也一樣可以正常運作.

後來, 發現在 Windows 8 底下用 Visual Studio 2012 寫 Windows Form 的程式時, 也可以正常運作. 似乎只有在 console 程式會無法運作.

比較一下兩者的程式碼, 發現 Windows Form 的程式會在 Main 前面加上一行:

[STAThread]

試著加上變成:

namespace Shell32Test
{
[STAThread]
class Program
{
static void Main(string[] args)
{
object shell = new Shell32.Shell();
}
}
}

這樣子居然可以正常執行了. 真是個奇怪的 bug.

Del.icio.us Furl HEMiDEMi Technorati MyShare
迴響
暱稱:
標題:
個人網頁:
電子郵件:
authimage

迴響

  

Bad Behavior 已經阻擋了 92 個過去 7 天試圖闖關的垃圾迴響與引用。
Power by LifeType. Template design by JamesHuang. Valid XHTML and CSS