仅在 Vista (.net) 中存在问题:从线程调用非托管 dll(Shell32.dll,函数:SHEmptyRecycleBin)

发布于 2024-07-22 06:24:36 字数 1043 浏览 1 评论 0原文

********平台:在 Vista(旗舰版或家庭/高级)中它不起作用,其他操作系统(xp、windows7)它起作用************

我正在清空回收站在线程内使用 c++.net(或 c#.net)。 当我直接这样做(没有线程)时,它就起作用了。 但如果使用线程则不会。 请观看下面的代码片段:

namespace EmptyRecycleBin_C{
enum RecycleFlags
{
  SHERB_NOCONFIRMATION = 0x00000001,
  SHERB_NOPROGRESSUI = 0x00000002,
  SHERB_NOSOUND = 0x00000004
};
public ref class Form1 : public System::Windows::Forms::Form{

[DllImport("Shell32.dll",CharSet=CharSet::Unicode)]
static System::UInt32 SHEmptyRecycleBin(IntPtr hwnd, String^ pszRootPath, RecycleFlags dwFlags);

private: void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
  Thread^ th = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::doEmpty));
  th->Start();
  //this->doEmpty(); // this line works just fine
}

private: void doEmpty()
{
  try{
        SHEmptyRecycleBin(IntPtr::Zero, String::Empty, RecycleFlags::SHERB_NOCONFIRMATION);
     }catch(Exception^ ex)
     {Diagnostics::Debug::Write(ex->Message);}
}
};
}

这里有什么问题......?

********Platform: in Vista(ultimate or home/premium) it does not work, other OS(xp, windows7) it works***********

I'm emptying recycle bin using c++.net(or c#.net) inside a thread. When i do this straight (without thread) it works. But if thread used it doesn't. Please watch the code snippet below:

namespace EmptyRecycleBin_C{
enum RecycleFlags
{
  SHERB_NOCONFIRMATION = 0x00000001,
  SHERB_NOPROGRESSUI = 0x00000002,
  SHERB_NOSOUND = 0x00000004
};
public ref class Form1 : public System::Windows::Forms::Form{

[DllImport("Shell32.dll",CharSet=CharSet::Unicode)]
static System::UInt32 SHEmptyRecycleBin(IntPtr hwnd, String^ pszRootPath, RecycleFlags dwFlags);

private: void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
  Thread^ th = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::doEmpty));
  th->Start();
  //this->doEmpty(); // this line works just fine
}

private: void doEmpty()
{
  try{
        SHEmptyRecycleBin(IntPtr::Zero, String::Empty, RecycleFlags::SHERB_NOCONFIRMATION);
     }catch(Exception^ ex)
     {Diagnostics::Debug::Write(ex->Message);}
}
};
}

whats the problem here...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛时过境迁 2024-07-29 06:24:36

难道是因为您创建的线程在默认安全上下文中执行,而不是在主线程的安全上下文中执行?

有关提示,请参阅有关 ExecutionContext 的文档。 您可以在线程上设置 ExecutionContext 并重试。

Could it be because threads you create execute in the default security context, not in the security context of the main thread?

See the doc on ExecutionContext for a hint. You can set the ExecutionContext on your thread and re-try.

多情出卖 2024-07-29 06:24:36

您是否从线程中调用了 CoInitialize?

它返回什么错误代码?

Have you called CoInitialize from your thread?

What error code is it returning?

£噩梦荏苒 2024-07-29 06:24:36

Shell 函数仅适用于 STA 线程,.NET 线程默认为 MTA。 您可以将线程设置为使用单单元线程:

th->SetApartmentState(ApartmentState::STA);
th->Start();

Shell functions work with STA threads only, and .NET threads are MTA by default. You can set the thread to use single-apartment threading:

th->SetApartmentState(ApartmentState::STA);
th->Start();
情绪少女 2024-07-29 06:24:36

我不知道为什么会发生这种情况,但是您尝试过其他线程方法吗? 例如BackgroundWorker组件或ThreadPool.QueueUserWorkItem? 错误仍然发生吗?

I don't know WHY that's happening, but have you tried other threading methods? Such as a BackgroundWorker component or ThreadPool.QueueUserWorkItem? Does the error still happen?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文