0

Ok, so I have this code written in C++ that call the function SetThreadDesktop. I call this function from a dll when running as System user on Windows XP.

When called from a C# console application, the function succeeds, but when called from a VB.NET console application, it doesn't. With GetLastError, I saw that the error message was this:

SetThreadDesktop failed with error 170: The requested resource is in use (translated from french)

How does it come that it works for a C# console app and not for a VB.NET one? Btw, it doesn't work for a winform app neither. Can any .NET master explain this to me? Is it a compiler option that I have to change?

Thanks

Community
  • 1
  • 1
GianT971
  • 4,385
  • 7
  • 34
  • 46
  • 2
    Need to see code. C# and vb do things differently. – Rocklan Mar 06 '12 at 11:49
  • @LachlanB The code does really nothing else than importing the dll and then call the function, in both C# and VB.NET – GianT971 Mar 06 '12 at 11:53
  • If the C# code works and the VB.NET does not then your VB.NET code is incorrect. As to the reason it doesn't work for a Win32 form, without your code, We can't help you with that problem. – Security Hound Mar 06 '12 at 12:22
  • The VB.NET code enters in the function of the DLL, cause various things are done before the call of SetThreadDesktop, so I don't see how the VB.NET code could be incorrect...Maybe I should post it so you can see? – GianT971 Mar 06 '12 at 13:37

2 Answers2

2

Try adding a <MTAThread> attribute on your (VB) Main method as explained here.

That's really the only thing that differs between C# and VB Console projects by default.

Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
1

Shouldn't you declare the extern function as public shared?

<DllImport("AltCtrlDelCpp.dll")> _
Public Shared Function SimulateAltControlDel() As Boolean
vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • Yes - dll import has to be applied onto a shared/static method. Of course, if the method is declared within a module then `shared` is implied. – VinayC Mar 06 '12 at 11:59