Click here to Skip to main content
15,888,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a doubt on CPP and C# Applications running on Windows Embedded Compact-7.
I am loading a library in c++ as

hNWLib = LoadLibrary(L"Sample.dll");
SampleFunc= (PFN_SampleFunc)GetProcAddress(hNWLib, L"SampleFunc");

SampleFunc();

And inside the Sample.dll in "SampleFunc()" function am accessing "DOMDOCUMENTPTR" class as follows

MSXML2::IXMLDOMDocument2Ptr doc;
MSXML2::IXMLDOMElementPtr root;

while am accessing any of the method inside "doc" object, as like "doc->async" or "doc->appendChild" it through exception on sample application as

Exception 'Raised Exception' (0xe06d7363): Thread-Id=075601c6(pth=a34d53b8), Proc-Id=078b022a(pprc=a34d5d20) 'SAMPLE_APP.EXE',
VM-active=078b022a(pprc=a34d5d20) 'SAMPLE_APP.EXE'
PC=400513f4(coredll.dll+0x000413f4) RA=81037648(kernel.dll+0x0000e648) SP=0002fa1c, BVA=0002fa50

And the unexpected thing is, When i created a sample C# application and loads the DLL as

[DllImport("Sample.dll", SetLastError = true)]
private static unsafe extern UInt32 SampleFunc();

SampleFunc();

It's working without any issue. It is creating "MSXML2::IXMLDOMDocument2Ptr" object and working fine.

So i have some doubts regarding above trials.

Why this different behaviour in C++ and C# application with same implementation??
(As my understanding there might not be any problem in loading the library and accessing the "SampleFunc()")
Is there have any issue regarding accessing mechanism of "MSXML2::IXMLDOMDocument2Ptr" class in C++ and C#??
(Already including the needed xml library)
Is there have any application permission kind of things??
(As my knowledge there is no application permissions in WEC7)

Actually i am confused with this unexpected behavior. Can anyone help me to find the reason...

Thanks and Regards
ASWIN PP

What I have tried:

I tried to access the API inside a DLL using C++ and C# application but C++ showing exception where C# works fine. Only difference between above trials is we are accessing function inside DLL through CPP and C# code
Posted
Updated 14-Mar-16 23:38pm
v2
Comments
OriginalGriff 15-Mar-16 3:20am    
How are you loading it? What is the exception? Error messages?
What type of C++ application is it?
How do you access the library from C#?

If you would have checked the return values of the API functions or used a high warning level for the compiler, you would have detected the error in this line:
SampleFunc= (PFN_SampleFunc)GetProcAddress(hNWLib, L"SampleFunc");

The function name argument is of type LPCSTR but you are passing a wide string. So you get a NULL pointer because there is no function with the name "S".

See also the GetProcAddress function (Windows)[^] in the MSDN (note the example code which uses the TEXT macro for the module name and a plain string for the function name).
 
Share this answer
 
Comments
ASWIN11th 15-Mar-16 7:38am    
Hi,
I appreciate your response and Actually the GetProcAddress that your mention is not returned NULL, and able to get the proper function pointer.
SampleFunc = (PFN_SampleFunc)GetProcAddress(hNWLib, L"SampleFunc");

I think since exception is inside "SampleFunc", no issue with "GetProcAddress".
Jochen Arndt 15-Mar-16 7:53am    
Are you sure? If there is a function or a variable named "S" in the DLL, a pointer to that would be returned.

The parameter must be a LPCSTR. You are passing a LPCWSTR. There might be also other errors. But passing the correct parameter is essential.
ASWIN11th 15-Mar-16 9:42am    
Yeah...Damn sure,i am getting "SampleFunc" function prints. This application is running on Windows Embedded Compact-7.
Serkan Onat 15-Mar-16 17:05pm    
Jochen

Take a look at this , there is a unicode version for CE and Embedded

https://msdn.microsoft.com/en-us/library/ee488623%28v=winembedded.70%29.aspx
Jochen Arndt 16-Mar-16 4:02am    
You are right. I overlooked that you are using Compact 7.

Another error source may be using the wrong calling convention (cdecl, fastcall, or stdcall). What convention is used by the DLL and what is used in your code (the typedef for SampleFunc).
I am far to be a specialist of C++ ( I used C++ since VC++ 4 up to 6 when I was tired of VB , but I have stopped since the release of VC# in 2002 ) but maybe this link could be useful.

A good rule for every code I am writing ( VC#/VB ) : don't forget the treatment of the exception.
I am using this code in VC# ( rarely modified since 2003 )
C#
/// <summary>
/// Method used to treat a ( generic ) Exception.
/// Sets the values of m_shortmessage and m_errormessage.
/// Increments m_errorscount with 1.
/// Writes m_errormessage in the application logfile ( if it has been created ).
/// The code of all the methods treating not generic exceptions is created from this method.
/// </summary>
/// <param name="p_ex">( generic ) exception to treat</param>
public  static void TreatException(Exception p_ex)
{
     Int32     _i  = 0;
     String    _s  = "";
     Exception _ex = null;

     ErrorsCount++;
     ShortMessage = "Error " + ErrorsCount.ToString() + " A (generic) exception occured in " + ErrorPlace;
     _s  = Nl + NowToString() + Nl;
     _s += "********** " + ShortMessage + " **********" + Nl;
     _s += "Message                        : " + p_ex.Message + Nl;
     _s += "GetBaseException               : " + p_ex.GetBaseException().Message + Nl;
     _s += "HelpLink                       : " + p_ex.HelpLink + Nl;
     _s += "Source                         : " + p_ex.Source + Nl;
     _s += "TargetType                     : " + p_ex.TargetSite.Name + Nl;
     _s += "Data                           : ";
     if ( p_ex.Data == null )
     {
          _s += "N/A" + Nl;
     }
     else
     {
          _s += Nl;
          foreach ( DictionaryEntry _de in _ex.Data )
          {
               _s += "  - Key   : " + _de.Key.ToString() + Nl;
               _s += "    Value : " + _de.Value.ToString() + Nl;
          }
     }
     _s += "InnerException                 : ";
     if ( p_ex.InnerException == null )
     {
          _s += "N/A" + Nl;
     }
     else
     {
          _s += Nl;
          _ex = p_ex.InnerException;
          _i = 1;
          while ( _ex != null )
          {
               _s += "  Inner" + _i.ToString().PadRight(2) + ":" + Nl;
               _s += "    - Message        : " + _ex.Message + Nl;
               _s += "    - Type           : " + _ex.GetType().FullName + Nl;
               _s += "    - Data           : ";
               if ( _ex.Data == null )
               {
                    _s += "N/A" + Nl;
               }
               else
               {
                    _s += Nl;
                    foreach ( DictionaryEntry _de in _ex.Data )
                    {
                         _s += "         - Key   : " + _de.Key.ToString() + Nl;
                         _s += "           Value : " + _de.Value.ToString() + Nl;
                    }
               }
               _ex = _ex.InnerException;
          }
     }
     _s += "StackTrace                     : " + Nl + p_ex.StackTrace + Nl;
     _s += "End of treatement of the error " + ErrorsCount.ToString() + Nl + Nl;
     ErrorMessage = _s;
     AppWriteLog(false , ErrorMessage);
}

AppWriteLog is a method writing the ErrorMessage value in an application logfile. Nl is ony the carriage linefeed of this logfile. And I am interested only on the most internal exception ( the last one ). Maybe complicated but I am avoiding to loose time. If a specific exception is often happening , don't hesitate to create a method which is treating it without forgetting the specific properties/methods of it. I have provided some code on the MSDN/Technet forums for Systm.Data.SqlClient.SqlException ( Line , LineNumber , Procedure , ... ) or Microsoft.SqlServer.Management.Smo.SmoException ( for the ProdVer and SmoExceptionType properties ) since 2006.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900