Call C (exposed) function from COBOL program - c

Some time ago, I had created a DLL to be used in another C program. Basically I exposed specific functions by using the following within my dll:
void __declspec(dllexport) MyFunc(myFirstArg, mySecondArg);
Then I added an external file (MyExposedDll.h) with all exposed functions and structures to the new C program and included it:
#include MyExposedDll.h
Now how can I use this dll (or mainly a dll) to a Cobol function? I need to expose a function that has two char* arguments and returns a boolean.
Thanks,
Sun

This should not be difficult in an IBM Z/OS environment with LE support.
Capture the boolean result using the
COBOL CALL RETURNING
form of the CALL statement. The string arguments are passed just as any other arguments in a COBOL CALL statement.
The only thing to be wary of is that C uses Null terminated strings whereas COBOL generally does not. You should review
how to handle null terminated strings in
COBOL.
Have a look at: Using COBOL DLLs with C/C++ programs this gives a really simple example showing a call to a C++ function returning a function pointer.
EDIT
I may have missed part of your question... When your COBOL program is linked-edited, you need to provide the your DLL IMPORT file so it can be bound. See linking DLL's.
EDIT 2
Based on your comments, I take it you are running your application on a Z/OS box. Visual Studio is a PC based product so I am guessing that you develop your code there but deploy it under Z/OS? In order to get the COBOL program to recognize your DLL you need to create a "side file" from your C program when it is compiled. This "side file" contains the DLL structures needed by the linker when the COBOL program is linked. You should be able to get the process worked out from the links provided above.

Related

GCC API to compile a program within a program?

Is there a GCC C API (or some other C API) that I can use within a C program to compile other C programs? Something more programmatic than exec("gcc") and having to parse text intended for man (not machines) (eg. for debugging).
More specifically, the idea is to have a C program modify its C source code at run-time and then replace its own running instance (or parts of it; say it's made of multiple dynamic libraries that can be reloaded) with the newly compiled version of itself.

Is it possible for a program written in C to download an external function and treat this external function as a compiled and linked shared object?

I am working on a program in C, and I am having trouble with libconfig.h. Because of this, I think if I could have my program download an external function from the Internet (using libcurl.h) and have my program treat it as a compiled and linked shared object, that would be perfect. It would need to work on all desktop platforms (Windows, Mac, and Linux), so no .dll's, and would have to be downloaded by the program, treated as a function, and then get deleted by the program. So, my question is: is that possible in C?
The reason that I need to download it separately is because the function would need to be updated regularly, and requiring the user to download a new version of the program regularly would defeat the purpose of the program.
Well the closest to what you ask for would be this
Download .so/.dll using curl
Dynamically load .so/.dll into your process
set up function pointer in your process to point to a function in .so/.dll
On Windows:
HMODULE handle = LoadLibrary("mylib.dll");
if (handle)
myfunc = GetProcAddress(handle, "myfunc");
To unload call
FreeLibrary(handle)
It decreases ref count, and the DLL is actually unloaded when ref count hits 0.
On Linux, check this post:
How do I load a shared object in C++?
You can't just treat it as compiled; you would have to do one of two things:
Actually compile it on the fly, then load it as a dynamic library, which requires ensuring that there is a compiler on the system and will probably cause an unholy mess of errors on the user end.
Build your own C parser to interpret the external function, which is no small feat.
Far simpler solution: just write a function that works and compile platform-specific versions of it into your binary (or a library, if you prefer) before shipping the product.
You could link a Python interpreter into your program and have it execute a Python version of your function.
This approach would actually work with different languages, such as Java, Ruby, etc.

calling c functions from lua script

I found some references on this but I was not able to make them work. I have a Debian box with mysql and mysql-proxy. I am intercepting the SQL queries with the LUA script.
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
print("error read (COM_QUERY)")
end
local query = packet:sub(2)
print ("query : " .. query )
//Transformation here
return proxy.PROXY_SEND_QUERY
end
I want to parse and process the query so I can rewrite it with some c functions I already have developed. I am trying to find the way to call this fucntions but the only way I have found asumes that the c MAIN function starts the LUA registering process.
Is there any way to make the LUA script call the function in a compiled C file?
Any example of how should I make (LUA) and receive (C) the call?
Extract from lua.org
When we say that Lua can call C functions, this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function, but they are neither portable nor robust.) ...
... for a C function to be called from Lua, we must register it, that is, we must give its address to Lua in an appropriate way.
You should have a look here
SWIG is a good option to generate bindings for you: www.swig.org. You create a .i file in which load your C headers, and SWIG generates all the binding code for you. You then compile the generated code, link it to your C library and Lua library, and in your script you put require 'yourCLibrary', you can do what you want. Very practical, and your wrapper can be used to access yourCLibrary from other languages like Python and C#.

Some general C questions

I am trying to fully understand the process pro writing code in some language to execution by OS. In my case, the language would be C and the OS would be Windows. So far, I read many different articles, but I am not sure, whether I understand the process right, and I would like to ask you if you know some good articles on some subjects I couldnĀ“t find.
So, what I think I know about C (and basically other languages):
C compiler itself handles only data types, basic math operations, pointers operations, and work with functions. By work with functions I mean how to pass argument to it, and how to get output from function. During compilation, function call is replaced by passing arguments to stack, and than if function is not inline, its call is replaced by some symbol for linker. Linker than find the function definition, and replace the symbol to jump adress to that function (and of course than jump back to program).
If the above is generally true and I get it right, where to final .exe file actually linker saves the functions? After the main() function? And what creates the .exe header? Compiler or Linker?
Now, additional capabilities of C, today known as C standart library is set of functions and the declarations of them, that other programmers wrote to extend and simplify use of C language. But these functions like printf() were (or could be?) written in different language, or assembler. And there comes my next question, can be, for example printf() function be written in pure C without use of assembler?
I know this is quite big question, but I just mostly want to know, wheather I am right or not. And trust me, I read a lots of articles on the web, and I would not ask you, If I could find these infromation together on one place, in one article. Insted I must piece by piece gather informations, so I am not sure if I am right. Thanks.
I think that you're exposed to some information that is less relevant as a beginning C programmer and that might be confusing you - part of the goal of using a higher level language like this is to not have to initially think about how this process works. Over time, however, it is important to understand the process. I think you generally have the right understanding of it.
The C compiler merely takes C code and generates object files that contain machine language. Most of the object file is taken by the content of the functions. A simple function call in C, for example, would be represented in the compiled form as low level operators to push things into the stack, change the instruction pointer, etc.
The C library and any other libraries you would use are already available in this compiled form.
The linker is the thing that combines all the relevant object files, resolves all the dependencies (e.g., one object file calling a function in the standard library), and then creates the executable.
As for the language libraries are written in: Think of every function as a black box. As long as the black box has a standard interface (the C calling convention; that is, it takes arguments in a certain way, returns values in a certain way, etc.), how it is written internally doesn't matter. Most typically, the functions would be written in C or directly in assembly. By the time they make it into an object file (or as a compiled library), it doesn't really matter how they were initially created, what matters is that they are now in the compiled machine form.
The format of an executable depends on the operating system, but much of the body of the executable in windows is very similar to that of the object files. Imagine as if someone merged together all the object files and then added some glue. The glue does loading related stuff and then invokes the main(). When I was a kid, for example, people got a kick out of "changing the glue" to add another function before the main() that would display a splash screen with their name.
One thing to note, though is that regardless of the language you use, eventually you have to make use of operating system services. For example, to display stuff on the screen, to manage processes, etc. Most operating systems have an API that is also callable in a similar way, but its contents are not included in your EXE. For example, when you run your browser, it is an executable, but at some point there is a call to the Windows API to create a window or to load a font. If this was part of your EXE, your EXE would be huge. So even in your executable, there are "missing references". Usually, these are addressed at load time or run time, depending on the operating system.
I am a new user and this system does not allow me to post more than one link. To get around that restriction, I have posted some idea at my blog http://zhinkaas.blogspot.com/2010/04/how-does-c-program-work.html. It took me some time to get all links, but in totality, those should get you started.
The compiler is responsible for translating all your functions written in C into assembly, which it saves in the object file (DLL or EXE, for example). So, if you write a .c file that has a main function and a few other function, the compiler will translate all of those into assembly and save them together in the EXE file. Then, when you run the file, the loader (which is part of the OS) knows to start running the main function first. Otherwise, the main function is just like any other function for the compiler.
The linker is responsible for resolving any references between functions and variables in one object file with the references in other files. For example, if you call printf(), since you do not define the function printf() yourself, the linker is responsible for making sure that the call to printf() goes to the right system library where printf() is defined. This is done at compile-time.
printf() is indeed be written in pure C. What it does is call a system call in the OS which knows how to actually send characters to the standard output (like a window terminal). When you call printf() in your program, at compile time, the linker is responsible for linking your call to the printf() function in the standard C libraries. When the function is passed at run-time, printf() formats the arguments properly and then calls the appropriate OS system call to actually display the characters.

How can I write a Windows application without using WinMain?

Windows GUI applications written in C/C++ have 'WinMain' as an entry point (rather than 'main'). My understanding of this is that the compiler generates a 'main' function to be called by the C Runtime. This 'main' function sets up the necessary environment for the GUI and calls into 'WinMain' (specifying the instance handles etc.).
In short, I believe console and GUI application startup to differ in the following way:
Console application:
C Runtime --> 'main' function (hand-coded)
GUI application:
C Runtime --> 'main' function (compiler-generated) --> 'WinMain' function (hand-coded)
I would like to both validate this understanding and find out how I can hand-code a Windows GUI with just a 'main' function (i.e. without having to write 'WinMain').
You have an incorrect understanding. The difference between main and WinMain, apart from some differet initialization code, is the parameters passed to it.
main looks like this:
int main(int argc, char* argv[]);
While WinMain looks like this:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);
Something has to setup those parameters and make the call, and that's the startup code. When you compile and link a program, one of the linker parameters is the entry point, and that will be, depending on a console or GUI app, a different bit of startup code.
You can certainly write your own startup code, just go into your visual c++ source directory and you can find the startup code, it's called crt0.c and it's in the VC\crt\src directory.
With Just main, you can not code Winmain. For justifications, Following statements were taken from
http://blogs.msdn.com/oldnewthing/archive/2007/12/03/6644060.aspx
[In Windows Programming,] Why wasn't the application entry point called
main? Well, for one thing, the name main was already taken, and
Windows didn't have the authority to reserve an alternate definition.
There was no C language standardization committee back then; C was
what Dennis said it was, and it was hardly guaranteed that Dennis
would take any special steps to preserve Windows source code
compatibility in any future version of the C language. Since K&R
didn't specify that implementations could extend the acceptable forms
of the main function, it was entirely possible that there was a legal
C compiler that rejected programs that declared main incorrectly. The
current C language standard explicitly permits implementation-specific
alternate definitions for main, but requiring all compilers to support
this new Windows-specific version in order to compile Windows programs
would gratuitously restrict the set of compilers you could use for
writing Windows programs.
If you managed to overcome that obstacle, you'd have the problem that
the Windows version of main would have to be something like this:
int main(int argc, char *argv[], HINSTANCE hinst,
HINSTANCE hinstPrev, int nCmdShow);
Due to the way C linkage was performed, all variations of a function had to agree on the
parameters they had in common. This means that the Windows version
would have to add its parameters onto the end of the longest existing
version of main, and then you'd have to cross your fingers and hope
that the C language never added another alternate version of main. If
you went this route, your crossed fingers failed you, because it turns
out that a third parameter was added to main some time later, and it
conflicted with your Windows-friendly version.
Suppose you managed to convince Dennis not to allow that
three-parameter version of main. You still have to come up with those
first two parameters, which means that every program's startup code
needs to contain a command line parser. Back in the 16-bit days,
people scrimped to save every byte. Telling them, "Oh, and all your
programs are going to be 2KB bigger" probably wouldn't make you a lot
of friends. I mean, that's four sectors of I/O off a floppy disk!
But probably the reason why the Windows entry point was given a
different name is to emphasize that it's a different execution
environment. If it were called main, people would take C programs
designed for a console environment, throw them into their Windows
compiler, and then run them, with disastrous results.
Hope this clears your doubts.
It works the other way. There's a statically linked object file which comes with the compiler that holds the actual entry point. That entry point does initialization and then calls your entry point (i.e. WinMain).
What that static part expects to call may be tweakable. For example, in Visual Studio there's a field for the entry point name in the linker settings.

Resources