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: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(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:trToStringAnsi(pEncoded);
delete pEncoded;
return encodedString;
}
static String* Decode(String* message)
{
char* pDecoded = (new
EncodeDecode(3))->Decode(String2Char(message));
String* decodedString =
Marshal:trToStringAnsi(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.
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: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(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:trToStringAnsi(pEncoded);
delete pEncoded;
return encodedString;
}
static String* Decode(String* message)
{
char* pDecoded = (new
EncodeDecode(3))->Decode(String2Char(message));
String* decodedString =
Marshal:trToStringAnsi(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.