Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Can a function in an .EXE file be accessed ? if so How it is possible with VC++

Can a function in an .EXE file be accessed ? if so How it is possible with VC++

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Jawahar   India  (2 posts)  Bio
Date Mon 17 Jul 2006 10:40 AM (UTC)
Message

Friends,

I am new to this forum, I have a doubt about, any function with in .EXE is accessible or not.If yes, Can anybody tell how to do that...
Top

Posted by Tzaro   USA  (41 posts)  Bio
Date Reply #1 on Mon 17 Jul 2006 02:06 PM (UTC)
Message
Welcome to Gammon Software Solutions Forum.

I'm slightly confused by your question. Are you wanting to examine the code, in C++, that's within the executable (perhaps to rewrite or see how someone else wrote it?)... are you wanting to debug the program and see what the name of a function is? etc.

I'm going to assume you want to actually see the computer code, as written in C++, to use for learning purposes or so you can rewrite it.
Once the program has been compiled, you can't really see the "computer code" in a programming format (unless you have the source files). You can, however, use a decompiler to translate the program back into a form of language, like C++. I haven't experimented too much with this but, from my standpoint, decompilers don't work all that well. It may, however, give you enough of the code to work with.

Several decompilers exist, but I won't pretend to know which ones are good vs which are not. You can do a google search on "C++ decompiler" and some will pop up. As far as I am aware, this is your best bet (and possibly the only solution to your problem).

I hope this was somewhat insightful. If this isn't what you were looking for, write back with a slightly more descriptive question.

for future reference, you might want to put this in the 'programming' forum. You'll probably get more responses that way :).

Implementer of Lost Prophecy,
Tzaro
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Mon 17 Jul 2006 02:59 PM (UTC)
Message
Tzaro is correct that if you only have the compiled source, you will have some difficulty seeing the textual source code. Decompilers (which translate assembly to source code) aren't really that useful unless you really, really know what you're doing, for several reasons:

  • you will get no meaningful variable names, thus removing the principal way of easily understanding what does what
  • optimizers can rearrange code, sometimes quite significantly, such that the original form is hard to recognize
  • unless the program was compiled with debug information left in, the symbol names (e.g. function names) will be gone; all you will know is that some function was called at some point


Why do you need to look at the source code? Maybe there's another way of doing whatever you need. Of course, the best bet would be to just get the source code, if you can.

And yes, this would have been more appropriate in the 'Programming: General' section.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Jawahar   India  (2 posts)  Bio
Date Reply #3 on Mon 24 Jul 2006 10:44 AM (UTC)
Message
TZARO sir, Thank you for your reply,

But my question is, can we use the functionality using function calling (like ordinary function call) (i.e, function in .exe file), as we normally do in other .Dll functions, is possible or not.

For calling a certain function in a dll file we will be including .lib file, and directly calling those functions.
Whether the same functionality is there or not for .exe files.

If possible, How it can be done.

Expecting your reply
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 24 Jul 2006 09:15 PM (UTC)
Message
I think not.

A library or DLL by definition is designed to expose its functions, that is its purpose.

An EXE is something that uses DLLs (perhaps), and basically only exposes one entry point (eg. WinMain or main).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Tzaro   USA  (41 posts)  Bio
Date Reply #5 on Mon 24 Jul 2006 10:29 PM (UTC)
Message
hmm... I think I got what you're asking now.

I'm definately far from an expert on this stuff. Nick Gammon, Ksilyan, or someone else of that caliber might be able to better answer.

However, I think it _MIGHT_ be possible. The only way I can think of accomplishing the task is to write a function in the dll that'll take a pointer to a table that stores the addresses of functions within the executable. While the executable is booting up, have it call upon the function in the dll file with the correct function name, address, etc. Then all you should need to do to call a function from within the executable is lookup the address of the function and send the appropriate data to the right spots.

I dunno if that made any sense or not. It made sense in my head :\

Tzaro

Implementer of Lost Prophecy,
Tzaro
Top

Posted by Tzaro   USA  (41 posts)  Bio
Date Reply #6 on Mon 24 Jul 2006 10:31 PM (UTC)
Message
Okay, so... while taking my sweet time figuring out how to best answer this question, Nick answered it... and probably did a much better job than I. I'd listen to him... he has more knowledge on programming in his left pinky than I do in my whole brain.

Tzaro

Implementer of Lost Prophecy,
Tzaro
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #7 on Tue 25 Jul 2006 05:00 PM (UTC)
Message
I'd go with Nick's answer. Executables aren't designed to be dynamically loaded up, so you can't get the functions that way, even though you might be able to see where the functions are if the program was compiled in debug mode. That being said, it is possible to have multiple exposed symbols in an executable, so that when it loads up a DLL, the DLL finds symbols in the executable. Of course, those are of a rather particular nature, and it's quite possible that in your case it will not work.

As I said, it might be better if you explained what you are trying to do, as there might be another way around this.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Jon Lambert   USA  (26 posts)  Bio
Date Reply #8 on Wed 26 Jul 2006 06:58 AM (UTC)

Amended on Wed 26 Jul 2006 07:09 AM (UTC) by Jon Lambert

Message
Yes. For example:


D:\vs98\vc>type prog1.c
int main(int argc, char** argv) {
}

int half(int i) {
  return i/2;
}

D:\vs98\vc>cl prog1.c /link/export:half
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

prog1.c
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:prog1.exe
/export:half
prog1.obj
   Creating library prog1.lib and object prog1.exp

D:\vs98\vc>tyep prog2.c
#include <windows.h>

typedef int (*FUNCTION)(int);
HMODULE hdl_exe;
FUNCTION fn_call;

int main (int argc, char** argv) {
  hdl_exe = LoadLibrary("prog1.exe");
  if (hdl_exe != NULL) {
     fn_call = (FUNCTION)GetProcAddress(hdl_exe, "half");
     if (!fn_call) {
        FreeLibrary(hdl_exe);
        printf("Error");
     } else {
        printf("return=%d",fn_call(5));
        FreeLibrary(hdl_exe);
     }
  }
}


D:\vs98\vc>cl prog2.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

prog2.c
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:prog2.exe
prog2.obj

D:\vs98\vc>prog2
return=2


Please note that you have to either have access to modify the source of the exec to export the function with dllexport or be able to link the exe as I did above.

It may be possible to handedit the export table in an executable, if you cannot relink it or don't have source access.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


28,129 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.