|
-
Mar 20th, 2002, 11:08 AM
#1
Thread Starter
New Member
problem with SetPriorityClass API
I'm trying to set the priority class for a process, but I keep receiving "Error 49: Bad DLL Calling Convention".
Here's the code I'm using. It works fine except for the last line which gives me the error.
Dim lngRetValStartDoc As Long
Dim pptName As String
Dim appName As String
pptName = "Microsoft PowerPoint - [C-Mac Slides_0503_English]"
'Initialize variables within a data type
With processAtt
.lpSecurityDescriptor = 0
.bInheritHandle = True
.nLength = Len(processAtt)
End With
With threadAtt
.lpSecurityDescriptor = 0
.bInheritHandle = True
.nLength = Len(threadAtt)
End With
appName = "C:\program files\microsoft office\office\powerpnt.exe"
'Create a new process
lngRetValCreateProcess = CreateProcess(appName, vbNullString, processAtt, threadAtt, _
False, CREATE_NO_WINDOW, Null, vbNullString, startUp, processInfo)
lngThreadID = processInfo.dwThreadId
lngProcessID = processInfo.dwProcessId
lngRetValStartDoc = StartDoc("C-Mac Slides_0503_English.ppt")
lngWindowName = FindWindow(vbNullString, pptName)
lngRetValGetPriority = GetPriorityClass(lngWindowName)
lngRetValSetPriority = SetPriorityClass(lngWindowName, NORMAL_PRIORITY_CLASS)
Microsoft Certified Professional
-
Mar 20th, 2002, 12:00 PM
#2
You are passing SetPriorityClass a window handle. It doesn't want a window handle, it wants a process thread. For that, you need to use the GetCurrentThread API. Here is an example from http://www.allapi.net/
VB Code:
Const THREAD_BASE_PRIORITY_IDLE = -15
Const THREAD_BASE_PRIORITY_LOWRT = 15
Const THREAD_BASE_PRIORITY_MIN = -2
Const THREAD_BASE_PRIORITY_MAX = 2
Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
Const THREAD_PRIORITY_NORMAL = 0
Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
Const HIGH_PRIORITY_CLASS = &H80
Const IDLE_PRIORITY_CLASS = &H40
Const NORMAL_PRIORITY_CLASS = &H20
Const REALTIME_PRIORITY_CLASS = &H100
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
'KPD-Team 2000
'URL: [url]http://www.allapi.net/[/url]
Dim hThread As Long, hProcess As Long
'retrieve the current thread and process
hThread = GetCurrentThread
hProcess = GetCurrentProcess
'set the new thread priority to "lowest"
SetThreadPriority hThread, THREAD_PRIORITY_LOWEST
'set the new priority class to "idle"
SetPriorityClass hProcess, IDLE_PRIORITY_CLASS
'print some results
Me.AutoRedraw = True
Me.Print "Current Thread Priority:" + Str$(GetThreadPriority(hThread))
Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hProcess))
End Sub
-
Mar 20th, 2002, 12:03 PM
#3
Oops sorry, I got it turned around. SetPriorityClass doesn't want a process thread, it wants a process, and the API that you need for that is GetCurrentProcess. The same example applies.
Sorry...
-
Mar 20th, 2002, 12:11 PM
#4
Thread Starter
New Member
Actually, it's not the current process I want to set the priority class for. I want to set the priority class of the process created with the CreateProcess function. Is that possible?
Thanks.
Microsoft Certified Professional
-
Mar 20th, 2002, 12:19 PM
#5
And the process created with CreateProcess isn't the current process, or at least will not always be the current process. Is this correct?
Assuming that it is, how about GetWindowThreadProcessId. This API
...retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.
-
Mar 20th, 2002, 01:11 PM
#6
Thread Starter
New Member
Maybe I should explain the bigger picture.
My VB app is opening a PowerPoint file in a separate process. I want to set the priority class of that process so PowerPoint can be forced to give up system resources when my app requires them.
The reason for doing this is because an error occurs whenever I click on my app while PowerPoint is doing something. PowerPoint sucks up 99% of my system resources and I want it to give some up when I need it. So I thought setting PowerPoint to a lower priority (I suspect MS has programmed it with a high priority) than my app would do the trick.
API programming is rather new to me. So I appreciate the help. By the way GetWindowThreadProcessID returns the same number as lngThreadID = processInfo.dwThreadId
Microsoft Certified Professional
-
Mar 20th, 2002, 03:03 PM
#7
I did a little research for you. Check out this thread, and the three links its displaying and see if you can find something that will meet you exact needs. 
If none of these do anything for you, let me know, and I'll see what else I can find.
http://www.codehound.com/vb/results/...ad+priority%22
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|