473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

FILE * stream in .DLL loaded from LoadLibrary() doesn't work

Hello,

I can use call a function with any arugment from LoadLibrary(),
but not a function with argument of "FILE*.

For example, I can build a .DLL dynamically loaded library with
option /DDD in VC++ in command link.exe.
This dynamically loaded library can then be loaded by
function LoadLibrary(). The address of a function say
func1(int) inside .DLL file can be exported and accessed by function
GetProcAddress(). I can then invokde the function inside
..DLL through a pointer to function. Also, the argument
can be passed from the main program to the function as shown below.

void *dll;
dll = LoadLibrary("mydll.dll");
void (*fp1)(int);
fp1 = (void (*)(int))GetProcAddress(dll, "func1");
fp1(10);

However, if the argument of a function say, func2() is
FILE *. Then, the program will crash as shown below.

FILE* s;
s = fopen("test")
void (*fp2)(FILE*);
fp2 = (void (*)(int))GetProcAddress(dll, "func2");
fp2(stdout); // or fp2(s) will crash.

It appears that the mydll.dll has separate FILE streams
and they are overlap with the application program with file
extension .exe. How can it be resloved?

Does anyone know how to handle this passing FILE* to
a function inside a .DLL loaded by LoadLibrary()?

Thank you in advance for your help.
Nov 16 '05 #1
8 3603
on********@yahoo.com wrote:
Hello,

I can use call a function with any arugment from LoadLibrary(),
but not a function with argument of "FILE*.

For example, I can build a .DLL dynamically loaded library with
option /DDD in VC++ in command link.exe.
This dynamically loaded library can then be loaded by
function LoadLibrary(). The address of a function say
func1(int) inside .DLL file can be exported and accessed by function
GetProcAddress(). I can then invokde the function inside
.DLL through a pointer to function. Also, the argument
can be passed from the main program to the function as shown below.

void *dll;
dll = LoadLibrary("mydll.dll");
void (*fp1)(int);
fp1 = (void (*)(int))GetProcAddress(dll, "func1");
fp1(10);

However, if the argument of a function say, func2() is
FILE *. Then, the program will crash as shown below.

FILE* s;
s = fopen("test")
void (*fp2)(FILE*);
fp2 = (void (*)(int))GetProcAddress(dll, "func2");
fp2(stdout); // or fp2(s) will crash.

It appears that the mydll.dll has separate FILE streams
and they are overlap with the application program with file
extension .exe. How can it be resloved?


Link both your EXE and all of your DLLs with the "Multi-threaded DLL"
runtime library (msvcrt.lib). You have two (or more) sets of FILE streams
because your application (when taken in total) has two (or more) copies of
the C runtime library in memory.

-cd
Nov 16 '05 #2
inline..

<on********@yahoo.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Hello,

I can use call a function with any arugment from LoadLibrary(),
but not a function with argument of "FILE*.

For example, I can build a .DLL dynamically loaded library with
option /DDD in VC++ in command link.exe.
This dynamically loaded library can then be loaded by
function LoadLibrary(). The address of a function say
func1(int) inside .DLL file can be exported and accessed by function
GetProcAddress(). I can then invokde the function inside
.DLL through a pointer to function. Also, the argument
can be passed from the main program to the function as shown below.

void *dll;
dll = LoadLibrary("mydll.dll");
void (*fp1)(int);
fp1 = (void (*)(int))GetProcAddress(dll, "func1");
fp1(10);

However, if the argument of a function say, func2() is
FILE *. Then, the program will crash as shown below.

FILE* s;
s = fopen("test")
void (*fp2)(FILE*);
Above you declare the function pointer fp2 to point to a function that takes
a FILE * argument.
fp2 = (void (*)(int))GetProcAddress(dll, "func2");
Above you cast the return from GetProcAddress to a pointer to a function
that takes an int argument. This conflicts with the FILE * you should be
working with.

Do yourself a favor and create a header file that is included in both
compilations. This is a fundamental principle of maintaining your sanity
when working out interfaces. Create a typedef for fp2 in your header file:

typedef void (*FP2PROC) (FILE *);

Use the typedef in your code:

FP2PROC fp2;
fp2 = (FP2PROC)GetProcAddress(...);
fp2(stdout); // or fp2(s) will crash.

It appears that the mydll.dll has separate FILE streams
and they are overlap with the application program with file
extension .exe. How can it be resloved?
I don't think your problem has to do with separate FILE streams or multiple
copies of the runtime.
Does anyone know how to handle this passing FILE* to
a function inside a .DLL loaded by LoadLibrary()?

Thank you in advance for your help.

Nov 16 '05 #3
Carl Daniel [VC++ MVP] wrote:
on********@yahoo.com wrote:
Hello,

I can use call a function with any arugment from LoadLibrary(),
but not a function with argument of "FILE*.

For example, I can build a .DLL dynamically loaded library with
option /DDD in VC++ in command link.exe.
This dynamically loaded library can then be loaded by
function LoadLibrary(). The address of a function say
func1(int) inside .DLL file can be exported and accessed by function
GetProcAddress(). I can then invokde the function inside
.DLL through a pointer to function. Also, the argument
can be passed from the main program to the function as shown below.

void *dll;
dll = LoadLibrary("mydll.dll");
void (*fp1)(int);
fp1 = (void (*)(int))GetProcAddress(dll, "func1");
fp1(10);

However, if the argument of a function say, func2() is
FILE *. Then, the program will crash as shown below.

FILE* s;
s = fopen("test")
void (*fp2)(FILE*);
fp2 = (void (*)(int))GetProcAddress(dll, "func2");
fp2(stdout); // or fp2(s) will crash.

It appears that the mydll.dll has separate FILE streams
and they are overlap with the application program with file
extension .exe. How can it be resloved?

Link both your EXE and all of your DLLs with the "Multi-threaded DLL"
runtime library (msvcrt.lib). You have two (or more) sets of FILE streams
because your application (when taken in total) has two (or more) copies of
the C runtime library in memory.


In my code, I had

extern char ** __cdecl environ;

Now, when I link the code with the .obj compiled with
option /MD based on your suggestion, it will complain that
the symbol _environ cannot be found. how to fix it?

Thank you for your help.


-cd


Nov 16 '05 #4
clyclopedic wrote:
inline..

<on********@yahoo.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Hello,

I can use call a function with any arugment from LoadLibrary(),
but not a function with argument of "FILE*.

For example, I can build a .DLL dynamically loaded library with
option /DDD in VC++ in command link.exe.
This dynamically loaded library can then be loaded by
function LoadLibrary(). The address of a function say
func1(int) inside .DLL file can be exported and accessed by function
GetProcAddress(). I can then invokde the function inside
.DLL through a pointer to function. Also, the argument
can be passed from the main program to the function as shown below.

void *dll;
dll = LoadLibrary("mydll.dll");
void (*fp1)(int);
fp1 = (void (*)(int))GetProcAddress(dll, "func1");
fp1(10);

However, if the argument of a function say, func2() is
FILE *. Then, the program will crash as shown below.

FILE* s;
s = fopen("test")
void (*fp2)(FILE*);

Above you declare the function pointer fp2 to point to a function that takes
a FILE * argument.

fp2 = (void (*)(int))GetProcAddress(dll, "func2");

Above you cast the return from GetProcAddress to a pointer to a function
that takes an int argument. This conflicts with the FILE * you should be
working with.


Thanks for your suggestion.

It is my typo with int.


Do yourself a favor and create a header file that is included in both
compilations. This is a fundamental principle of maintaining your sanity
when working out interfaces. Create a typedef for fp2 in your header file:

typedef void (*FP2PROC) (FILE *);

Use the typedef in your code:

FP2PROC fp2;
fp2 = (FP2PROC)GetProcAddress(...);

fp2(stdout); // or fp2(s) will crash.

It appears that the mydll.dll has separate FILE streams
and they are overlap with the application program with file
extension .exe. How can it be resloved?

I don't think your problem has to do with separate FILE streams or multiple
copies of the runtime.


use carl's suggestion to compile with /MD works.

Thanks.

Does anyone know how to handle this passing FILE* to
a function inside a .DLL loaded by LoadLibrary()?

Thank you in advance for your help.



Nov 16 '05 #5
on********@yahoo.com wrote:
In my code, I had

extern char ** __cdecl environ;

Now, when I link the code with the .obj compiled with
option /MD based on your suggestion, it will complain that
the symbol _environ cannot be found. how to fix it?


IIUC, you should be using the _putenv() and _getenv() rather than accessing
_environ directly.

-cd
Nov 16 '05 #6
Carl Daniel [VC++ MVP] wrote:
on********@yahoo.com wrote:

In my code, I had

extern char ** __cdecl environ;

Now, when I link the code with the .obj compiled with
option /MD based on your suggestion, it will complain that
the symbol _environ cannot be found. how to fix it?

IIUC, you should be using the _putenv() and _getenv() rather than accessing
_environ directly.


These two funcs can be used only when you know the variable names.
If not, _environ contains all and should be used.

Thanks. I have fixed this problem.

-cd


Nov 16 '05 #7

Link both your EXE and all of your DLLs with the "Multi-threaded DLL"
runtime library (msvcrt.lib). You have two (or more) sets of FILE streams
because your application (when taken in total) has two (or more) copies of
the C runtime library in memory.
After linking with /MD, it requires to have MSVCR71.dll
in Win 9x and Win2000 to be able to run. Does it mean that MSVCR71.dll
is required to distribute with binary code together for machines with
no .NET installed?

Thanks.


-cd


Nov 16 '05 #8
on********@yahoo.com wrote:
Link both your EXE and all of your DLLs with the "Multi-threaded DLL"
runtime library (msvcrt.lib). You have two (or more) sets of FILE
streams because your application (when taken in total) has two (or
more) copies of the C runtime library in memory.


After linking with /MD, it requires to have MSVCR71.dll
in Win 9x and Win2000 to be able to run. Does it mean that MSVCR71.dll
is required to distribute with binary code together for machines with
no .NET installed?


msvcr71.dll should be distributed with your application - it's not part of
..NET, but rather part of VC++. You were able to get away with not
distributing the runtime DLL with VC6 applications because msvcrt.dll is
also included in the operating system itself.

-cd
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Barry | last post by:
Hello, I hope somebody can help me with the following problem. My application requires a jar file be loaded dynamically at runtime, which I do using a URLClassLoader. However, the classes and...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
5
by: Drew Yallop | last post by:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by passing cursor over the stream reader object)the format is a perfect replica of the...
2
by: sanu | last post by:
Hi, I have a dll declared in my VB.Net Webservice as below Declare Function fntest Lib "test.dll" _ Alias "TEST_FUNC" (ByVal a As Integer, ByVal b As Integer, ByVal c As Single) As Double ...
11
by: MrNobody | last post by:
I need to zip up an 8Gb file using C# - do you know of any freeware libraries I can use to accomplish this? I guess it needs to be Zip64 to support such file sizes. I tried something called...
2
by: kbutterly | last post by:
Good afternoon, I have what appears to be a caching issue, but i'm a bit of a newbie at http headers so it may be something else. I have a small asp.net 2.0 application that is called to serve...
2
by: pigmartian | last post by:
(my apologies if this is a repost, but it sure seems like the first attempt disappeared into the ether...) I'm writing a program that uses functionality from two different sets of cdlls which...
3
by: Paddy | last post by:
Hi, I am am falling at the first hurdle when trying to access a library using ctypes. I have a file libucdb.so which the file command says is shared object, but I cannot get it to load: Any...
8
by: raylopez99 | last post by:
I have the latest version of Visual Studio 2008 Professional, which allows you to create resource files (this is the .resx file, no?), unlike the Express version, which does not. I am trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.