Solved

Parameters for CreateProcess API call are giving type mismatch

Posted on 1998-03-06
14
460 Views
Last Modified: 2008-02-01
I'm trying to use CreateProcess to spawn an application.
For some reason, one of the arguments for it is giving me a type mismatch error.  I have checked and double checked and I can find no mismatch.  Further, VB doesn't tell me which argument is yielding the type mismatch, it just highlights the entire API call.  If you could tell me why CreateProcess won't buy this, I would appreciate it.

Here are my type declarations, api declarations, and the offending line:

The offending line:
CreateProcessA(0&, strCommandLine, 0&, 0&, 1&, _
"high_priority_class", 0, 0&, usrStartupInfo, _ usrProcessInfo)

The API declaration:
Declare Function CreateProcessA Lib "kernel32" _
    (ByVal lpApplicationName As Long, _
     ByVal lpCommandLine As String, _
     ByVal lpProcessAttributes As Long, _
     ByVal lpThreadAttributes As Long, _
     ByVal bInheritHandles As Long, _
     ByVal dwCreationFlags As Long, _
     ByVal lpEnvironment As Long, _
     ByVal lpCurrentDirectory As String, _
     lpStartupInfo As StartupInfo, _
     lpProcessInformation As PROCESS_INFORMATION) _
     As Long _

And the variable declarations










 Dim strCommandLine As String
 Dim usrStartupInfo As StartupInfo        
 Dim usrProcessInfo As PROCESS_INFORMATION

 Dim lonCProcRTN As Long                  
 Dim strStartupDir As String
0
Comment
Question by:marimba
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1458600
                       
                       
Private Declare Function CreateProcess Lib "kernel32" Alias _
      "CreateProcessA" (ByVal lpApplicationName As String, ByVal _
      lpCommandLine As String, lpProcessAttributes As Any, _
      lpThreadAttributes As Any, ByVal bInheritHandles As Long, _
      ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal _
      lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, _
      lpProcessInformation As PROCESS_INFORMATION) As Long

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1458601
marimba, you can use the vb API viewer to check you API declaration, If you have VB5 professional or enterprise version.
In Win95/WinNT, goto Start/Program/Microsoft VB5/API text viewer, and when the viewer show up, open the file WinAPI32.txt, select the API fuction you want, the declaration will appear, then you can just copy it to your VB code.
This is what I copied:
Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


0
 
LVL 9

Expert Comment

by:Dalin
ID: 1458602
Just a reminder, the STARTUPINFO must be defined.


Private Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessId As Long
   dwThreadId As Long
End Type


Private Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   dwX As Long
   dwY As Long
   dwXSize As Long
   dwYSize As Long
   dwXCountChars As Long
   dwYCountChars As Long
   dwFillAttribute As Long
   dwFlags As Long
   wShowWindow As Integer
   cbReserved2 As Integer
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type

If it doesn't work out, please add to comments.
                       

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1458603
marimba, I hope you reject Dalin's proposed answer because your declaration is fine.

The problem is that the dwCreationFlags argument must be passed a long value. HIGH_PRIORITY_CLASS is a constant and has a value of &H80 (decimal 128).  Your declaration is correct although you probably should delete the underscore at the end (see note below).  Place the path & filename in strCommandLine, add the HIGH_PRIORITY_CLASS constant declaration and call CreateProcess as follows:

Const HIGH_PRIORITY_CLASS = &H80

CreateProcessA 0, strCommandLine, 0, 0, 1, _
HIGH_PRIORITY_CLASS , 0, 0, usrStartupInfo, usrProcessInfo

Note: The underscore at the end of the declaration tells VB that the next line is part of the declaration... and since that line is blank... it works out.  I would remove that underscore.  You're call to CreateProcessA also had an underscore character in with usrProcessInfo - this underscore shouldn't be there.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1458604
Thank you for your comments, mrmick.
Also thanks to cliffABB for his contribution.

marimba, When an API call gives you a mismatch error, you either declared wrong or passed a paramenter with the wrong type.

when I first see your question, I saw you delcared ApplicationName as Long. So I thought I will post the standard VB declaration first, so that you can check your parameters against it, and I will then follow up.  When I come back a few minute late, cliffABB already posted his comments to point out the wrong time in "high_priority_class". So I did not repeat that, instead, I give you a general method of get the right declaration, and was hoping if you have further problem after taking into consideration of the comments from me and cliffABB.

One thing on the API is that these API fuctions are writting in a language other than VB. So some of the types may not be the original type for various reason. So sometimes even a seemly wrong type may work.

Anyway, how is your program goes?  I hope our comments helped. If you have anymore problems, please let us know.

My main goal is to offer help. I will do my best in getting a solution for the questions I answer.  If you feel that my answer and comments did not help, you can reject my answer as mrmick suggested.  Either way, as long as you get your probelem solved, I will be happy. I enjoyed the experience and looking forward to work with you in to get the best solution for this problem or any other you may have in the further.

Best wishes.

Dalin
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1458605
Dalin,

You may be trying to help find the solution, but assuming that you do know what you're doing... do you recognize that (clifABB noticed the problem) I posted the solution?  And if your goal is really just to help... why are you still trying to get credit for this question knowing that you didn't provide the solution?  In fact, you’re the cause of marimba having to wait several days for a solution because you locked this question and took it out of the "Questions Awaiting Answers" list.

Probably the best think you can do in helping others Darlin is this: If you don't know the answer to a question, post anything you may have to say as a comment.  Post only SOLUTIONS as answers to questions.  I (and probably others) would have noticed the problem and posted the solution on the same day this question was posted had it been in the "Questions Awaiting Answers" list.

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1458606
mrmick:

Thank you again for your comments and critism. It is a good suggestion to me and others to post comments when they don't think they can provide a good solution. I will certainly do my best for that.

Please forgive me if I made any mistakes in understanding what the procedure is for this site.  I am now here and certainly make a mistake on my first try.

One thing I do want to clearify is that I am not so concerned about the credit, at least not yet, becuase I don't see the importance to me yet.  I am doing what you suggestted to do (obviously not enough), is to post comments when I don't believe I can give the solution.  In some cases even I had the solution I onlt post as comments when I was the first one to show in the question. When I get an answer, I will post it regardless of it's open status. If I make a mistake, I sincerely apologize.  You may check the following questions to see what I am saying:
1. Run Simulteneousely  by X factor, still open
2. CommandButtom by mroh, Locked
3. Capture Screen from VB, you had a comments on it.

You have the right to believe that I am just try to get credit.  I can understand.   But I am confident that you will change your mind when time goes on.  

Thanks again, and  I do respect you from my heart.

Dalin  

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Expert Comment

by:Dalin
ID: 1458607
Dalin, I was just frustrated by several questions that I found to be locked without an answer.  I felt that you made a stab in the dark because you didn't obviously have any idea what was causing the problem at the time you locked the question.  Granted, you may have realized 10 minutes later but not when you locked the question.  I just wanted to make a point with you partially because I had seen comments you had made in another question suggesting that someone else had wrongfully done this.  I'd be happy to continue this discussion VIA email.  My address is: mick@owen.wa.com

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1458608
Gentlemen:  I appreciate your help.  Being that this was a 3 day weekend for me, I have not yet had time to test all the solutions.  But I will do so first thing tomorrow.

However, I did get to look at the first two portions before I left on Friday.  Dalin, I appreciate your help, but this was a 300 point question, and cutting and pasting an API declaration as well as the type definitions are not 300 point answer material.  I cut and pasted it myself, it's not like I pulled it from memory and needed someone to check my recall.  The expertise in knowing API calls is what each argument is and how it is passed, and that is why I am confessing ignorance about that here in this forum.  


0
 

Author Comment

by:marimba
ID: 1458609
marimba.  Thank you for your comments.
Sorry I did not fully understand your problem before I post the answer and I sincerely appologize to you and other viewers.
If you have any further questions, please let us know.
If it work out, mrmick and cliffABB deserved all the credits.
Best wishes.
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1458610
marimba, I think you will find that the solution I posted is what you're looking for.

Dalin, I think your participation here is good and I hope I haven't discouraged you.

0
 
LVL 8

Accepted Solution

by:
mrmick earned 300 total points
ID: 1458611
MrMick you are correct sir.  It was indeed the value of the lpProcessInformation flag was not passed correctly.

Could I beg one more related question...

I know that a lot of these API's ask for a wide variety of constants such as the one you described.  If I had known where to find the constant for the high priority class, I might have stumbled across the answer on my own.  I didn't find this list in the Win32 SDK nor in Appleman's Win32 API book, can you tell me where I can find a list of these constants?

Thanks again,
marimba
0
 

Author Comment

by:marimba
ID: 1458612
Also gentlemen, I have found an error in this which we all should be aware of, and nobody has yet caught.  This did not affect me because I was passing this value as 0 anyway, but if I were using this parameter, it would have been a problem.

There's a misspelling in the declaration found in the API text viewer.  It is for the definition of lpCurrentDirectory, this is cut-and-pasted straight from the API Text viewer as-is:

Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
0
 

Author Comment

by:marimba
ID: 1458613
miramba, the names of ALL the passed parameters could be misspelled and the function would still work.  They mean nothing to the compiler.  Those names are only for you're benefit.  Try it, change all the names by copying the function definition, commenting out the good one, change all the names of the parameters on the uncommented one, and run your app.  It will still work fine.  The names that matter are the alias name (in the above posts you weren't using alias so in that case the function name) and the library name ("kernal32").  In fact, the function name (alias since you're using it here) is case sensitive.

Constant declarations are available in the API Viewer.  When you open the API file, the default API Type is "Declares" in the dropdown combo box.  Change the value to "Constants".  You can get the definitions of the structures like SECURITY_ATTRIBUTES or STARTUPINFO or PROCESS_INFORMATION also by changing the API Type to: "Types"

The constants definitions are not available in the Win32 SDK; however, they are shipped with it.  In the same root path as the infoviewer (usually MSTools) there is an "Include" folder.  Inside this folder are C header files.  You can sometimes determine the correct header file by deriving it from the library name, but usually you can get it form the "Quick Info" button in the Win32 API function reference.  In this case the quick info displays "kernel32" as the library and "winbase.h" as the header file.  The "winbase.h" header file defines the following (among many other definitions):

#define NORMAL_PRIORITY_CLASS       0x00000020
#define IDLE_PRIORITY_CLASS         0x00000040
#define HIGH_PRIORITY_CLASS         0x00000080
#define REALTIME_PRIORITY_CLASS     0x00000100

The VB translation looks like this:
Const NORMAL_PRIORITY_CLASS = &H20
Const IDLE_PRIORITY_CLASS = &H40
Const HIGH_PRIORITY_CLASS = &H80
Const REALTIME_PRIORITY_CLASS = &H100

The "0x" at the beginning of a number in a C header file means hexadecimal.  In the VB definitions, the "&" means Long Integer Value and the "H" means Hexadecimal.  The Win32 SDK lists the above possible constant names for dwCreationFlags parameter.  Notice the "dw" in "dwCreationFlags".  This indicates a "Double Word" which equals two words each consisting of two bytes; thus, 4 bytes.  Constants are normally expressed in ALL CAPS.

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now