473,399 Members | 3,401 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,399 software developers and data experts.

file opening in a mixed-mode solution

I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?

Lee
Mar 31 '06 #1
11 1576
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?


Just a guess... but most likely, you've misdiagnosed where the exception is
coming from, because you're absolutely right - getting a managed exception
out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming from?

-cd
Mar 31 '06 #2
Lee Crabtree wrote:
I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?

Lee


Some experimentation has shown that it's got something to do with where
the file is located. If it's in the same directory as the executable
(so that the path is ".\\file.ext"), it works. Likewise, if the file is
in the root (so that the path is "c:\\file.ext"), there's no problem.
However, in any other folder (where the path is something like
"c:\\dir\\file.ext"), there's a System.NullReferenceException.

I've checked permissions already, so that's not it. I'm completely at a
loss here.

Lee
Mar 31 '06 #3
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?


Just a guess... but most likely, you've misdiagnosed where the exception is
coming from, because you're absolutely right - getting a managed exception
out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming from?

-cd


I'm stepping through one at a time, and that line is what always
triggers it.

Lee
Mar 31 '06 #4
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:up**************@TK2MSFTNGP14.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?


Just a guess... but most likely, you've misdiagnosed where the exception
is coming from, because you're absolutely right - getting a managed
exception out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming from?

-cd


I'm stepping through one at a time, and that line is what always triggers
it.


Interesting. Have you tried doing something like using CreateFile to open
the file instead? I can't think of any reason why it should make any
difference, but you're in wierd territory here.

-cd
Mar 31 '06 #5
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:up**************@TK2MSFTNGP14.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL, and a
C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On top of
all that, both the path and mode are correctly set.

What am I doing wrong?
Just a guess... but most likely, you've misdiagnosed where the exception
is coming from, because you're absolutely right - getting a managed
exception out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming from?

-cd

I'm stepping through one at a time, and that line is what always triggers
it.


Interesting. Have you tried doing something like using CreateFile to open
the file instead? I can't think of any reason why it should make any
difference, but you're in wierd territory here.

-cd

Not yet. I put a wee test app together to try and see if managed C++
got in the way of fopen or something. It's never been the case in the
past, but when you have no other options... Anyway, it was no good. It
worked just like it always has (that's odd...the fact that it worked is
somehow BAD).

I'm getting a flat forehead.

Lee
Mar 31 '06 #6
Lee Crabtree wrote:
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL,
and a C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On
top of all that, both the path and mode are correctly set.

What am I doing wrong?


Just a guess... but most likely, you've misdiagnosed where the
exception is coming from, because you're absolutely right - getting
a managed exception out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming
from? -cd


I'm stepping through one at a time, and that line is what always
triggers it.


Is the offending line compiled to native or CLR?

Make sure you debug in mixed or native mode. My guess is that there is
something wrong with the native code (though you haven't shown enough code
for us to spot it), whoses neat effect is to generate a
NullReferenceException when switching back from nativeto managed. But what
you are seing is probably the symptom, not the cause.

Arnaud
MVP - VC
Apr 2 '06 #7
Arnaud Debaene wrote:
Lee Crabtree wrote:
Carl Daniel [VC++ MVP] wrote:
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I've got a solution that has a C++ project that compiles to a DLL,
and a C# project that is the main piece of the program.

The DLL is managed C++ on top of regular C++. In one of the native
functions, I make a call that looks a little like this:

file = _fsopen(path, mode, SH_DENYNO);

That call generates a System.NullReferenceException, which makes
ABSOLUTELY no sense, since it's happening in unmanaged code. On
top of all that, both the path and mode are correctly set.

What am I doing wrong?
Just a guess... but most likely, you've misdiagnosed where the
exception is coming from, because you're absolutely right - getting
a managed exception out of a native library function makes no sense.

Are you absolutely 100% sure that's where the exception is coming
from? -cd

I'm stepping through one at a time, and that line is what always
triggers it.


Is the offending line compiled to native or CLR?

Make sure you debug in mixed or native mode. My guess is that there is
something wrong with the native code (though you haven't shown enough code
for us to spot it), whoses neat effect is to generate a
NullReferenceException when switching back from nativeto managed. But what
you are seing is probably the symptom, not the cause.

Arnaud
MVP - VC


I've done some more testing, and it's the act of opening a file that's
causing the exception. Anywhere in my unmanaged code, I can do
something like

FILE* file = fopen("C:\\file", "rb");

and I generate an exception. What gives?

Lee
Apr 3 '06 #8
Okay, I have officially coded myself into a straitjacket.

I have two projects in this solution (as I said). One is a C# form app,
and the other is a mixed-mode DLL. When I set the debugging mode of the
C# app to allow unmanaged debugging, the fopen executes without a hitch.
When I turn off unmanaged debugging, I get the NullReferenceException.

Lee
Apr 3 '06 #9
Lee Crabtree wrote:
Okay, I have officially coded myself into a straitjacket.

I have two projects in this solution (as I said). One is a C# form
app, and the other is a mixed-mode DLL. When I set the debugging
mode of the C# app to allow unmanaged debugging, the fopen executes
without a hitch. When I turn off unmanaged debugging, I get the
NullReferenceException.


What happens without any debugger?

Also, if you debug with "allow unmanaged" turned on, are you sure, it's not
some native code *after* the file opening that triggers the exception? Can
you debug step-by-step the unmanaged code until you go back to the managed
caller?

Arnaud
MVP - VC
Apr 3 '06 #10
Arnaud Debaene wrote:
Lee Crabtree wrote:
Okay, I have officially coded myself into a straitjacket.

I have two projects in this solution (as I said). One is a C# form
app, and the other is a mixed-mode DLL. When I set the debugging
mode of the C# app to allow unmanaged debugging, the fopen executes
without a hitch. When I turn off unmanaged debugging, I get the
NullReferenceException.


What happens without any debugger?

Also, if you debug with "allow unmanaged" turned on, are you sure, it's not
some native code *after* the file opening that triggers the exception? Can
you debug step-by-step the unmanaged code until you go back to the managed
caller?

Arnaud
MVP - VC


I found it, finally. I turned on the "allow unmanaged" debugger and
went looking for some other possible problem. I found out that my
predecessor had used a strcpy to copy the entirety of a file into a
buffer. The problem was, you guessed it, that the buffer wasn't always
big enough. That wasn't a problem for them, because the files they were
working with were always small. I, however, had to enlarge the size of
said file, and boom.

In the end, a liberal usage of asserts and some pointer arithmetic saved
the day.

Let this be a lesson to us all - never trust the debugger. It's handy,
it points out a lot of things, but it'll never be able to track things
down to your satisfaction. If I'd stopped and used my gray matter, I
might have found this earlier and saved myself several hours of pain.
Oh well. We live and learn. At any rate, we live.

Lee

P.S. Thanks for the debugger setting. I'd never even noticed that one.
Apr 3 '06 #11
Lee Crabtree wrote:
I found it, finally. I turned on the "allow unmanaged" debugger and
went looking for some other possible problem. I found out that my
predecessor had used a strcpy to copy the entirety of a file into a
buffer. The problem was, you guessed it, that the buffer wasn't
always big enough. That wasn't a problem for them, because the files
they were working with were always small. I, however, had to enlarge
the size of said file, and boom.

In the end, a liberal usage of asserts and some pointer arithmetic
saved the day.

Let this be a lesson to us all - never trust the debugger. It's
handy, it points out a lot of things, but it'll never be able to
track things down to your satisfaction. If I'd stopped and used my
gray matter, I might have found this earlier and saved myself several
hours of pain. Oh well. We live and learn. At any rate, we live.

Ah, the good ol'one stack overflow !!!! ;-) I milit for an internal code
policy that FORBIDS strcpy, raw pointer arithmetic and the like... STL gives
you about the same flexibility but is much more secure...

Arnaud
MVP - VC
Apr 4 '06 #12

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

Similar topics

3
by: Perttu Pulkkinen | last post by:
No questions, but just consider if this is useful to you:-) but of course feedback & corrections are welcome. function php_mixed_to_js_value($jsname, $mixed) { if(is_null($mixed)) { return "\n...
5
by: Vasilis Serghi | last post by:
Hi all, I am fairly new to C programming and I have come to a stand still. I am trying to create a program that will accept a user input and give the user an output depending on the contents of a...
10
by: John | last post by:
Hi When I open a new outlook email from vb.net, sometimes outlook is very slow to appear or occasionally outlook freezes completely. I am targeting mixed office2000/xp environments so I am...
6
by: john | last post by:
The standard method to transmit a file from an aspx page to a browser is to stream the file to the response then end the response. The HTML code generated by the aspx page is discarded, and the...
6
by: tactics40 | last post by:
I'm writing a GUI application in Jython which takes a database text file and performs some operations on the data, finally spitting out a group of CSV files. I generate a log file and several...
1
by: nukephp | last post by:
Hello Guys would you know about php email with his attachment Working on linux platform................................................... The code as below: mail.php <?php //define the...
2
by: Fabian Braennstroem | last post by:
Hi, I would like to delete a region on a log file which has this kind of structure: #------flutest------------------------------------------------------------ 498 1.0086e-03 2.4608e-04...
3
by: Paul H | last post by:
I have a transactions table and a balance table that look something like this: tblTransactions TransactionID (PK Autonumber) ClientID TransactionDate TransactionAmount (currency field, values...
34
by: xiao | last post by:
Does anyone tried to open a file with extension of .lat using C? why I cannot open these files using fopen? Any other way that I can try to open it? Thank you~
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.