How to get the current calling process's name in AS400

I am trying to move a windows DLL to the AS400 platform. There is a function called GetModuleName() in the current DLL file, what this funcation does is to get the application's name of the current process which is calling the DLL file.  However, I couldn't find a equivalent function in the AS400, I would be very appreciated if anybody could provide a sample code that can do the same thing as the GetModuleName() does. Thanks
rxue_expertexchangeAsked:
Who is Participating?
 
tliottaConnect With a Mentor Commented:
Process name isn't exactly meaningful under OS/400. I'd first guess what you want is job name instead. Alternatively, perhaps you want to retrieve the name of the program that calls getpid()?

Example place to start for job name might be Qp0wGetJobID()--Get Qualified Job Name and ID for Process ID. Get the pid with getpid() and send it to Qp0wGetJobID().

Now, getting the "name" of a program needs a bit of clarification. An object name has essentially three parts: library, name and type. The fully-qualified name of a program can change while the program is running. The type will always be *PGM, but the library qualifier can change for a number of reasons, e.g., a program moving into QRPLOBJ because it was changed while active. Likewise, the unqualified name can change simply by a rename operation.

Those may be unlikely, but they might need to be accounted for depending on exactly why you need them within the program while it runs.

Commonly, you'd create a function that starts by sending a message to the caller. The function then immediately receives that message from the caller's queue and extracts the name attribute from the message and returns that back to the caller. That will generally give you the unqualified name of the program object. You might name this function GetModuleName(). You can create this as a CL procedure using basic CL commands or create it in C if you wish using message handling APIs. Unless it's going to be called a lot, it probably doesn't matter. In either case, I'd create it as a procedure rather than a program. But I also see above that you really want a function -- returning a function value. Since CL procedures cannot return function values, you'll want to use C rather than CL.

A fundamental flaw with this is that the library portion of the fully-qualified object name isn't available.

There are "better" ways of finding the program object's name, but the available techniques change with different releases of OS/400. Since you seem to be distributing to multiple, possibly unknown releases, and since you probably don't want to be getting too deep into MI programming quite yet, sending/receiving a message is probably best. A CL procedure is easy for this. To get fully-qualified name without resorting to MI gets ugly, and I think Dave has already alluded to that.

Unfortunately, I don't have a C example. But if you're clear on what you want and the examples Dave has referenced seem to describe the info you need, we can supply the APIs you'll want to use.

Tom
0
 
daveslaterCommented:
0
 
daveslaterCommented:
Ps
if you need to get the entire program stack I have a routine - but it has a larger overhead

Dave
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rxue_expertexchangeAuthor Commented:
Thanks, Dave
The DLL is written in C. I am a newbie in AS400, and don't know much about the code you posted. Is there anyway to convert those code to C, or Is there any functions in AS400 like getpid(), getprocessname() I can call?
0
 
daveslaterCommented:
Hi

one thing first do you want module or program name, if it is actual module (ie from a service program that has multiple modules) the I will need to post a different answer.

I do not know much about C so it will take a 2 step solution.
I hope that you now how to call another program in C passing either 1 or 2 parms

The programs are CL programs that will be in source file QCLSRC

the first call will get your current program id
see accepted answer
http://www.experts-exchange.com/Programming/Programming_Platforms/AS400/Q_20873205.html
Note the line in the accepted answer
DCL        VAR(&PGM) TYPE(*CHAR) LEN(10) VALUE('WCM')  
the value is what you will call the program ie I have compile the CL to program WCM

then use this value to call the CL program above this will then get the program that called your C program


Because I am not that good with C and you are new the the 400 it may take a bit but we will get there :-)

Dave
0
 
rxue_expertexchangeAuthor Commented:
Hi, Dave

Does that mean I have to create the extra CL program, and then call the CL program from the C to get the calling process's name. If so, I get some problems on that:
1) Not quite sure how to get the return value(which is the calling process's name in this case) from CL program in C;
2) When we deploy this solution to other servers, we have to deploy one more extra CL program.

0
 
daveslaterCommented:
Hi
You can do it with a system API, but this is a bit slower and I will need to dig out the code. The plus side is that it will be a function that can be included like other C functions as a service program.

It will take me a few days as I am a bit busy at work

In anyone else can do it faster please join in.

Dave
0
 
rxue_expertexchangeAuthor Commented:
Hi, Dave

System API might be my prefered way. I don't worry too much about it's slower, since it will be called only once during the initialization. Actually, I just found there is an API called "getpid()" in AS400, but still don't know how to figure out the process name from this API.
Take your time, Dave, I am not urgent on this right now, because I can't test it out untill I get the access to the AS400 in the next week or the week after next. Once I get the access, I will also try your way mentioned above, if it works and there is no better way, I might accept it.
Thanks for your time.
0
 
tliottaConnect With a Mentor Commented:
You can use the following:

  #include <sys/types.h>
  #include <unistd.h>

  pid_t getpid(void);

This and related APIs are documented under the UNIX-type APIs for OS/400.

0
 
rxue_expertexchangeAuthor Commented:
But you can only get the process ID from the getpid(), how can I get the process name attached with the process ID?
0
 
rxue_expertexchangeAuthor Commented:
Thank you all for your advices, guys
I am still waiting for the access to the AS400 system and then I can test out your ideas. I will definitely let you know how it is going after I start my testing.
0
 
rxue_expertexchangeAuthor Commented:
After poking around on the AS400 system, finally got some ideas about it and you guys' comments got more meaningful to me, Thanks.
What I really need is the program name, and don't care about the library.
The Qp0wGetJobID() API is good for me and works. So I will accept Tom's comment.
Dave's way could be a good solution, but I am really not familar with CL and don't know how to combine it with my C code seamless, so I haven't try it yet.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.