C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 9th, 2003, 05:42 AM
bsuribabu bsuribabu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Hyderabad
Posts: 17 bsuribabu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to bsuribabu
Running a Object file

Hi,

I am working on Turbo C for DOS .

First I have made a sample.c which contains sample() function and i have generated the objected file sample.obj.

In another file ( test.c) i have opened this file with fopen and this address is casted to funcp function pointer and called the that function pointer.

Is this the correct way of doing? Is this the way how DLLs are work in core level ( Actually i dont know how DLLs work in background )

Following is the my source code


First File :

#include <stdio.h>
void main()
{
printf("Hai Suri");
return;
}

Second File :

#include <stdio.h>

FILE *fp;

void main()
{
void (*funcp)();
fp = fopen("sample.obj","rb");
if ( !fp )
printf("File Cannot be opened");
else
{
funcp = ( void (*)()) fp;
funcp();
}
fclose(fp);
}

anyway this program is running but going in infinite loop


Please clarify me .

Reply With Quote
  #2  
Old January 9th, 2003, 11:09 AM
S. de Valk S. de Valk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 18 S. de Valk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

I am not sure if I fully understand your question.

If I get it right, you're asking how to call a function in an object file. I have no knowledge of Turbo C, but I think you should specify the name of the object file on the command line. I assume Turbo C offers a flag through which the linker knows what object file(s) to include in the executable. Perhaps it has the form of something like this:

dosprompt> [name of your compiler] [your C source].c -l[name of the object file]

(check the Turbo C documentation for the correct notation).

This means that you can simply call sample() in your main(), without the need to cast it to a function pointer first.

Hope this helps.

Reply With Quote
  #3  
Old January 9th, 2003, 04:31 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,313 Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 23rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 4 Days 4 h 23 m 47 sec
Reputation Power: 2529
Well, you do have the general idea correctly. However, I don't think that DOS ever supported the idea of DLLs. IIRC, the closest that it came to was using overlay functions. Search google if you want more information on overlay functions. Frankly, if you're just learning C, it's probably easier to just statically link the two files together like the previous poster mentioned. That way, you don't have to indirect through a pointer.

As to why your code is hanging, you aren't really reading the code from the file into memory, so you can't indirect to the function via the file pointer. You probably need to read the file first into a memory buffer and then point the pointer to it. Also, an OBJ file contains machine code, but it also contains some stub headers for the linker to use, so you can't arbitrarily point a function pointer to the first byte of the OBJ file buffer, you need to find the area in the file where the function's code starts. Then again, like I said above, DOS never really supported DLLs, so you might want to see how it is done under Windows or *Nix. The reason why most people used overlays was because DOS was limited to 640K and when you're using overlays, you could unload the overlay library when you were done using it.

Under Windows or *Nix, the process is pretty much close to what you described. In Windows, it would go something like this:
Code:
int (*pFunc)();
/* Load the library into memory */
HINSTANCE *hLib = LoadLibrary("mylib"); 

/* Find address of the function we want, in the dynamic library */
pFunc = GetProcAddress(hLib, "MyFunctionName"); 

/* Execute the function */
if (pFunc)
   (*pFunc)();

/* Free the library */
FreeLibrary(hLib);


Under *nix, the logic for shared libraries is similar and the functions corresponding to LoadLibrary, GetProcAddress and FreeLibrary are dlopen(), dlsym() and dlclose(). Hope this helps!

Last edited by Scorpions4ever : January 9th, 2003 at 04:35 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Running a Object file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
Stay green...Green IT