Results 1 to 7 of 7

Thread: problem with SetPriorityClass API

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Location
    Hamilton Ontario Canada
    Posts
    4

    Question 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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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:
    1. Const THREAD_BASE_PRIORITY_IDLE = -15
    2. Const THREAD_BASE_PRIORITY_LOWRT = 15
    3. Const THREAD_BASE_PRIORITY_MIN = -2
    4. Const THREAD_BASE_PRIORITY_MAX = 2
    5. Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
    6. Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
    7. Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
    8. Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
    9. Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
    10. Const THREAD_PRIORITY_NORMAL = 0
    11. Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
    12. Const HIGH_PRIORITY_CLASS = &H80
    13. Const IDLE_PRIORITY_CLASS = &H40
    14. Const NORMAL_PRIORITY_CLASS = &H20
    15. Const REALTIME_PRIORITY_CLASS = &H100
    16. Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
    17. Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
    18. Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
    19. Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
    20. Private Declare Function GetCurrentThread Lib "kernel32" () As Long
    21. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    22. Private Sub Form_Load()
    23.     'KPD-Team 2000
    24.     'URL: [url]http://www.allapi.net/[/url]
    25.     'E-Mail: [email][email protected][/email]
    26.     Dim hThread As Long, hProcess As Long
    27.     'retrieve the current thread and process
    28.     hThread = GetCurrentThread
    29.     hProcess = GetCurrentProcess
    30.     'set the new thread priority to "lowest"
    31.     SetThreadPriority hThread, THREAD_PRIORITY_LOWEST
    32.     'set the new priority class to "idle"
    33.     SetPriorityClass hProcess, IDLE_PRIORITY_CLASS
    34.     'print some results
    35.     Me.AutoRedraw = True
    36.     Me.Print "Current Thread Priority:" + Str$(GetThreadPriority(hThread))
    37.     Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hProcess))
    38. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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...

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Location
    Hamilton Ontario Canada
    Posts
    4
    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

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Location
    Hamilton Ontario Canada
    Posts
    4
    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

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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
  •  



Click Here to Expand Forum to Full Width