NullReferenceException in call from c# class to c++ static class when touch web.config file

A

AAguiar

Thanks for your replies. Last week, I continued working with this problem.
Trying to reproduce the error, I developed a short example and found that
the problem is related to strong name dll.

Following is the example to reproduce it.

I have a C++ project (mixed dll, managed and unmanaged), with classes:
EncodeDecode.cpp, EncodeDecode.h, DecoderRing.cpp as follows:

---------------------------
//EncodeDecode.cpp:

#include "stdafx.h"
#include <string.h>
#include "DecoderRing1.h"
#include "EncodeDecode.h"
EncodeDecode::EncodeDecode(int theKeyOffset)
{
keyOffset = theKeyOffset;
}
char* EncodeDecode::Encode(char* pMessage)
{
char* pEncoded = new char[strlen(pMessage) + 1];
char* pDest = pEncoded;
char* pSource = pMessage;
while (*pSource != '\0')
{
*pDest = *pSource + keyOffset;
pSource++;
pDest++;
}
*pDest = '\0';
return pEncoded;
}
char* EncodeDecode::Decode(char* pMessage)
{
char* pDecoded = new char[strlen(pMessage) + 1];
char* pDest = pDecoded;
char* pSource = pMessage;
while (*pSource != '\0')
{
*pDest = *pSource - keyOffset;
pSource++;
pDest++;
}
*pDest = '\0';
return pDecoded;
}
---------------------------
//EncodeDecode.h
#pragma once
__nogc
class EncodeDecode
{
private:
int keyOffset;
public:
EncodeDecode(int theKeyOffset);
char* Encode(char* message);
char* Decode(char* message);
};
---------------------------
//DecoderRing.cpp
// This is the main DLL file.
#include "stdafx.h"
#include <string.h>
using namespace System;
#include "EncodeDecode.h"
#include "DecoderRing.h"
using namespace System::Runtime::InteropServices;
class String2Char
{
char* m_sptr;
public:
String2Char(String* s)
{
IntPtr* m_ptr =__nogc new
IntPtr(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s));
m_sptr = strdup((char*)m_ptr->ToPointer());
delete m_ptr;
}
~String2Char()
{
free(m_sptr);
}
operator char* ()
{
//return (m_sptr =
(char*)m_ptr.ToPointer());
return (m_sptr);
}
};
public __gc class DecoderRing
{
public:
static String* Encode(String* message)
{
char* pEncoded = (new
EncodeDecode(3))->Encode(String2Char(message));
String* encodedString =
Marshal::ptrToStringAnsi(pEncoded);

delete pEncoded;
return encodedString;

}
static String* Decode(String* message)
{
char* pDecoded = (new
EncodeDecode(3))->Decode(String2Char(message));
String* decodedString =
Marshal::ptrToStringAnsi(pDecoded);
delete pDecoded;
return decodedString;
}
};
---------------------------
// DecoderRing.h
#pragma once
#include <stdlib.h>
---------------------------

In this project, Confguration properties->C/C++->Command line=
/O2 /AI "Release" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_WINDLL" /FD /EHsc
/GS /Fo"Release/" /Fd"Release/vc70.pdb" /W3 /nologo /c /clr /TP /FU
"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll" /FU
"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /FU
"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll"

Confguration properties->Linker -> Command Line=
/OUT:"Release\EncodeDecode.dll" /INCREMENTAL:NO /NOLOGO /DLL
/INCLUDE:"__DllMainCRTStartup@12" /DEBUG /PDB:"Release/EncodeDecode.pdb"
/NOENTRY /FIXED:No msvcrt.lib kernel32.lib user32.lib gdi32.lib
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib
uuid.lib odbc32.lib odbccp32.lib
Aditional options= /noentry /NODEFAULTLIB:LIBCMT
This project make the EncondeDecode.dll

I have a second dll: MyCrypto.dll
//MyCrypto.cs
using System;
namespace ManagedDll
{
public class DataStoreUtil_2
{
public static string LoadDataStores(string msg)
{
return (new MyCrypto()).Decode(msg);
}
}
public class MyCrypto
{
public string Decode(string msg)
{
string res1=DecoderRing.Encode(msg);
return res1;
}
}
}
I make MyCrypto.dll with MyCrypto.rsp=
/t:library
/w:0
/debug
/out:bin\MyCrypto.dll
/r:bin\log4net.dll
/r:Mixed\Release\EncodeDecode.dll
/r:system.dll
MyCrypto.cs
AssemblyInfo.cs (**)

And I have a dll for the aspx=
//simplehandler.cs
using System.Web;
using System;
using ManagedDll;
namespace Programs
{
public class simplehandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string res1 =
DataStoreUtil_2.LoadDataStores("test");
context.Response.Write("<br>res1=" + res1 +
"<br><br> ");
context.Response.Write("<br><br>Hello
World");

}
catch(Exception e)
{
context.Response.Write("<br>error=" +
e.Message + "<br><br> " + e.StackTrace);
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
The web.config file=
<configuration>
<system.web>
<trace enabled="true" />
<identity impersonate="true" />
<httpHandlers>
<add verb="*" path="*.aspx"
type="Programs.simplehandler,simplehandler" />
</httpHandlers>
</system.web>
</configuration>

To reproduce the error:
1) Go to http://localhost/services/simplehandler.aspx (this is OK, you can
see the message "hello world"),
2) Restart the aspnet_wp process (windows task manager),
3) Press F5 in page 1) (this is OK)
4) Modify web.config file (Add to it a blank space for example),
5) F5 in page 1) => The following error appears = Object reference not set
to an instance of an object. at ManagedDll.MyCrypto.Decode(String msg).
This error (step 5) only appears when AssemblyInfo.cs (**) have a
AssemblyKeyFile (is a strong name dll), but doesn't appear when it isn't a
strong name dll.

Any help or direction regarding the resolution of this problem is welcomed.
Thanks in advance.
 
S

Steven Cheng[MSFT]

Hi AAguiar,

Thank you for using Microsoft Newsgroup Service. Currently, I found that
this post is a duplicated one with another one in the queue, and I'll reply
you in the other post. Please feel free to follow in that post.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

AAguiar

Thanks Steven.

It's true that I sent this same post with a different subject, but then I
thought that it would be better to write with the same subject like in the
olders emails so as to continue the original thread.

I'll be waiting for any reply in the other post.
Thank you.
 

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

Top