Share via

Correct Declare advise needed

Peter Volz 1,295 Reputation points
2023-07-28T21:26:16.27+00:00

Hello

Is this declare in my VB.NET Main startup form correct?

<DllImport("User32.dll", SetLastError:=True)>
Private Shared Function LockWorkStation() As Boolean
End Function
<DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
Friend Shared Function GetTickCount() As UInteger
End Function
<DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
Friend Shared Function SetThreadExecutionState(ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
End Function

I've no idea if DllImport(Kernel32.dll) should be repeated twice for GetTickCount and SetThreadExecutionState?

Because if I remove the 2nd DllImport, I will get:

Function 'SetThreadExecutionState' doesn't return a value on all code paths.

My original working declare was:

Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE

Thanks for advise :)

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Castorix31 91,876 Reputation points
2023-07-29T08:13:41.9966667+00:00

Yes, you must add DllImport for each function declaration

See MSDN : DllImportAttribute Class (click VB on top-right ComboBox for code in VB...)

or a small sample in VB I had uploaded recently which uses some APIs with several DllImport :https://github.com/castorix/VB_TransparentControl/blob/master/TransparentControl.vb

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.