DLLInport from a ASP.NET web app using C#

G

Guest

I am trying to do some calls to unmanaged DLL that I have written. I am doing
it in the Session_Start of my Global.asax.cs.
The first time I call a function in my DLL it returns correct string value.
The return type in unmanaged DLL is LPSTR (tried LPTSTR too). There are no
in/out params in the functions, just a return value.
The second time (and onwards) when I try to make a call to the same
function, I get an exception "Object reference not set to an object....)." I
am trying this by loading multiple page requests to the web app.
Any ideas where I should look into to resolve this one.

Thanks
Ravneet
 
G

Guest

Scott,
that **can** be a very faint case. The unmanaged code works fine as far as i
have tested. But still to rule out this case I can do a repeated call to the
unmanaged code to see if it works (from unmanaged code itself).
As for the case from managed code, the call returns correct string value on
the first attempt, and fails with an exception in successive calls.
 
S

Scott Allen

Ok, perhaps you could post some of your code. Do you know exactly what
line throws the null reference exception? You should be able to get
that information from a stack trace.
 
G

Guest

Hi Scott,
A sample follows:

<Managed code snippet begins>

public string getSerial()
{
string serial = "";
try{
serial = getSerialEx(); //this line throws NullPointerException
}
catch(Exception npe){
string s = npe.StackTrace;
}
return serial;
}
</managed code snippet ends>

The serial is returned correctly in first run. Keeping the first session on,
I open another browser and browse to the app. This time the call fails,
giving an exception at the line marked above.
On the umanaged code side, the function just returns a LPSTR (tried with
LPTSTR too). Initially the function was taking a LPTSTR out parameter, and I
was using StringBuilder argument in the managed code, but this kept on
returning garbage to me. Played with Charset.XXXX too to fix that but didnt
help much, so changed the unmanaged function to accept no parameters but
return a LPSTR now.
Let me know if you need some more info.

Thanks !
 
G

Guest

Hi Scott,
I will run the CLRSpy to narrow the cause. Meanwhile the sig of the native is:

LPSTR APIENTRY getSerialEx();

it is declared in the managed code as:
static extern string getSerialEx();

What I am not able to understand is that why it works the first time around
and fails in successive sessions. I must remind you that this code is being
called from Session_Start of the ASP.NET web application. To narrow down
issues if I do a LoadLibrary just before the call it returns all the time
with a valid module handle. So the DLL is loaded but the call to the function
fails from second instance onwards. Now, this sounds like some memory not
being released issue. I am gonna do some more tests, meanwhile if you (or
anyone else) has something to suggest, pls do.

Thanks
Ravneet
 
S

Scott Allen

Yes - it does sound odd. Is there anychance something in this unmanged
method uses thread local storage? If that is the case it would have to
be invoked on the same thread everytime, else exhibit wierd behavior.
 
G

Guest

I dont think there is any threal local storage involved in the C++ code. I
will have to confirm this from the author of the DLL though. As far as I can
see it is just a registry call that gets done in the unmanaged code, to get a
string entry, and returned back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top