Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a native C++ dll and a virtual class("mycert") in it.I have an ATL dll project and i included the native dll into the ATL and create an interface for the 'mycert' .In mycert interface i have a function :
STDMETHODIMP Cmycert::setSignature(BYTE* data, SHORT size)
{

        cert = new mycert();
	ByteArray* barray = new ByteArray();
	for(int i=0;i<size;i++)
		(*barray)[i] = data[i];
	ASN1BitString* bitString = new ASN1BitString();
	bitString->setValue(*barray);
	cert->setSignature(bitString);
	
	return S_OK;
}


and in a c# project i include the ATL dll and call the above function:
byte[] data = new byte[3];
data[0] = 4;
data[1] = 6;
data[2] = 8;
mycertClass obj = new mycertClass();
obj.setSignature(ref data[0], 3);

when i run this code, it gives the following error:

"Runtime Error!

Program:
...dio 2008\Projects\myApplication\myApplication\bin\Debug\myApplication.vshost.exe

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information."

my native c++ dll has lots of classes other than 'mycert' and they are not virtual, i can do the same operations on them successfully ,only mycert class cause this error. Am I missing something about virtual classes?
Can you help me please?
Posted
Updated 3-Mar-10 3:44am
v2

Can you call your function
if it returns S_OK only ? :)
 
Share this answer
 
yes i can call it. I am calling the functions of the other interfaces but only this interface doesn't work.
 
Share this answer
 
OK, it is not just a bit :)

Could you explain
why do you need a new mycert()

or what would you like to have in your function ? :)
 
Share this answer
 
v2
mycert is the class in my native c++ dll so i should create an instance of it to use the functions of it . I don't create the instance of the mycertclass in the ATL with "new mycert()" code. ByteArray and ASN1BitString classes are also in the native dll and i can do operations with them, there is no problem with them. I am trying to call the setSignature function of the virtual mycert class in native C++ dll.
 
Share this answer
 
Could you post the definition of the mycert class ? :)
 
Share this answer
 
#ifdef dll_EXPORTS
#define dll_API __declspec(dllexport)
#else
#define dll_API __declspec(dllimport)
#endif
class dll_API mycert : public Sequence
{
    public:
        mycert(void);
        mycert(string name);
        ~mycert();
        void setTCert(TCert* tCertVal);
        void setSigAlgorithm(AlgId* sigAlgVal);
        void setSignature(ASN1BitString* sigVal);
        TCert* getTBSCert();
        AlgId* getSigAlgorithm();
        ASN1BitString* getSignature();
        ASN1Base* makeCopy(void);
        bool decode(ByteArray input);

    private:
        TCert* tbsCert;
        AlgId* sigAlg;
        ASN1BitString* sign;
        void setUpElements();
};

i have only header files of the dll, it is not my own dll.
 
Share this answer
 
It does not look to be a virtual class... :)

Is there an error
when you reduce the interface function to ? :
{
  mycert cert;
  return S_OK;
}


If you will get no error -
please read the documentation of mycert::setSignature(..)

(may be there is a limitation by the data length or format)
and try to call it with more "real" data :)

Elsewise I would test the DLL (mycert and mycert::setSignature)
from a C++ application directly, without any interface :)
 
Share this answer
 
v2
I got no error with that code.
I change the code :
STDMETHODIMP Cmycert::setSignature(BYTE* data, SHORT size)
{   
        cert = new mycert();
	ByteArray barray;
	for(int i=0;i<size;i++)
		barray[i] = data[i];
	ASN1BitString* bitString = new ASN1BitString();
	bitString->setValue(barray);
	cert->setSignature(bitString);
	return S_OK;
}


because definition of setValue function of ASN1String class
" void setValue(ByteArray) "
but still it gives the same error.
Thank you very much for your help.
 
Share this answer
 
You could also place an AfxMessageBox(_T("#<N>"));

after each line in this function to determinate the error source approximately :)
 
Share this answer
 
I put the line AfxMessageBox(_T("#<N>")) ,it gives the following error:

"fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] c:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afx.h "
 
Share this answer
 
:) ...then MessageBox(NULL, _T("Line #<N>"), _T("We are inside :)"), MB_OK);
 
Share this answer
 
It worked. Thanks Eugen! There is a problem inside the for loop, but i can't understand ,same loop works correctly in other functions.
 
Share this answer
 
OK :) Please try this body:
{
  BYTE byTest = data[1];
  return S_OK;
}
 
Share this answer
 
it is working, what should i do?
 
Share this answer
 
May be you should use ByteArray::Add(..) and not [..] ? :)
 
Share this answer
 
there is no Add function, there is an init function but it is protected.
 
Share this answer
 
Could you post the definition of the class ? :)
 
Share this answer
 
class dll_API ByteArray
{
  public:
      ByteArray(int sz = 0);
      ByteArray(const ByteArray&);
      ~ByteArray();
      ByteArray& operator=(const ByteArray&);
      unsigned char& operator[](int)  throw (RangeException);
      string getHexString(bool);
      string getValueAsString();
      int getSize();
      static ByteArray concat(ByteArray,ByteArray);
      static bool copy(ByteArray, int, ByteArray &, int, int);
      static int compare(ByteArray, ByteArray);

  protected:
      void init(const unsigned char*, int);
      string char2Hex(unsigned char ch);

  private:
      unsigned char* data;
      int size;
};
 
Share this answer
 
OK :) Try to construct your instance as following:
{
...
  ByteArray arTemp(size); // pass the second parameter here :)
  // The loop
...
}
 
Share this answer
 
;) it works, Eugen you are perfect. i am very thankful to you
 
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