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.cp p, EncodeDecode.h, DecoderRing.cpp as follows:
---------------------------
//EncodeDecode.cp p:
#include "stdafx.h"
#include <string.h>
#include "DecoderRing1.h "
#include "EncodeDecode.h "
EncodeDecode::E ncodeDecode(int theKeyOffset)
{
keyOffset = theKeyOffset;
}
char* EncodeDecode::E ncode(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::D ecode(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(in t 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 "DecoderRin g.h"
using namespace System::Runtime ::InteropServic es;
class String2Char
{
char* m_sptr;
public:
String2Char(Str ing* s)
{
IntPtr* m_ptr =__nogc new
IntPtr(System:: Runtime::Intero pServices::Mars hal::StringToHG lobalAnsi(s));
m_sptr = strdup((char*)m _ptr->ToPointer()) ;
delete m_ptr;
}
~String2Char()
{
free(m_sptr);
}
operator char* ()
{
//return (m_sptr =
(char*)m_ptr.To Pointer());
return (m_sptr);
}
};
public __gc class DecoderRing
{
public:
static String* Encode(String* message)
{
char* pEncoded = (new
EncodeDecode(3) )->Encode(String2 Char(message));
String* encodedString =
Marshal::PtrToS tringAnsi(pEnco ded);
delete pEncoded;
return encodedString;
}
static String* Decode(String* message)
{
char* pDecoded = (new
EncodeDecode(3) )->Decode(String2 Char(message));
String* decodedString =
Marshal::PtrToS tringAnsi(pDeco ded);
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\Mic rosoft.NET\Fram ework\v1.1.4322 \mscorlib.dll" /FU
"C:\WINDOWS\Mic rosoft.NET\Fram ework\v1.1.4322 \System.dll" /FU
"C:\WINDOWS\Mic rosoft.NET\Fram ework\v1.1.4322 \System.Data.dl l"
Confguration properties->Linker -> Command Line=
/OUT:"Release\En codeDecode.dll" /INCREMENTAL:NO /NOLOGO /DLL
/INCLUDE:"__DllM ainCRTStartup@1 2" /DEBUG /PDB:"Release/EncodeDecode.pd b"
/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:LI BCMT
This project make the EncondeDecode.d ll
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()).Dec ode(msg);
}
}
public class MyCrypto
{
public string Decode(string msg)
{
string res1=DecoderRin g.Encode(msg);
return res1;
}
}
}
I make MyCrypto.dll with MyCrypto.rsp=
/t:library
/w:0
/debug
/out:bin\MyCrypt o.dll
/r:bin\log4net.d ll
/r:Mixed\Release \EncodeDecode.d ll
/r:system.dll
MyCrypto.cs
AssemblyInfo.cs (**)
And I have a dll for the aspx=
//simplehandler.c s
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.Respons e.Write("<br>re s1=" + res1 +
"<br><br> ");
context.Respons e.Write("<br><b r>Hello
World");
}
catch(Exception e)
{
context.Respons e.Write("<br>er ror=" +
e.Message + "<br><br> " + e.StackTrace);
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
The web.config file=
<configuratio n>
<system.web>
<trace enabled="true" />
<identity impersonate="tr ue" />
<httpHandlers >
<add verb="*" path="*.aspx"
type="Programs. simplehandler,s implehandler" />
</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.MyCr ypto.Decode(Str ing 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.