C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
using c++ DLL with template class Posted by NoiK00 on 6 Aug 2010 at 8:15 AM
Hey,

I've recently tried to use an existing C++ library.. Now i was trying to test test the library but i´m struggling with "string subscript out of range". I've just created a console project to test the output. The output that is "coming out" the function is of a template class. In this scenario it is a std::string(atleast what i think). When running the main with the calls to the dll, everything but this variables works.

When i ignore the assertion error in debug mode, i can see that the variable has the correct values eventually i printed it to console and it was okay.

Anyone has experience whit this situation. Below i have a example of the class:
template <class BODY>
class TPGPointer
{
public:
	typedef BODY BodyType;		// The type of the body.

protected:
	BodyType* m_pBody;

	TPGPointer(void) : m_pBody(NULL) {;}
		// Constructs a handle instance with a pointer value of NULL. Throws no
		// exceptions. 

	TPGPointer(BODY* pBody) : m_pBody(pBody) {;}
		// Constructs a handle instance that points to the specified body. Throws no
		// exceptions. 

	TPGPointer(const TPGPointer<BODY>& _second) : m_pBody(_second.m_pBody) {;}
		// Constructs a handle instance that points to the same body as the second.
		// Throws no exceptions. 

	TPGPointer<BODY>& operator=(BODY* ptr) {
		// Assigns a new pointer value to the handle. The parameter is BODY*. Throws no
		// exceptions. 
		this->m_pBody = ptr;
		return *this;
	}

public:
	~TPGPointer(void) {;}
		// Destructor. Throws no exceptions.

// Public Member Operators
....

// Public Member Functions
	bool is_valid(void) const {
		// Returns true if pointer is non-nil (NULL); otherwise returns false. Throws
		// no exceptions. 
		return (this->m_pBody != NULL);
	}

	void swap_with(TPGPointer<BODY>& _second) {
		// Swap bodies, if any, with another handle. Throws no exceptions. 
		BodyType* tmpptr = this->m_pBody;
		this->m_pBody = _second.m_pBody;
		_second.m_pBody = tmpptr;
	}

	void validate(void) const {
		// Check if the pointer is valid, throwing an TPGException exception
		// if the pointer is NULL. Throws an TPGException exception. 
		if (!this->is_valid()) throw CTPGException("Invalid pointer");
	}

protected:
// Protected Member Function
	BODY* get(void) const {
		// Retrieves the handle's pointer value without validating it. Throws no
		// exceptions. 
		return this->m_pBody;
	}
};


I hope this is enough information....If not i've added my VS solution

Thanks
Attachment: managed.zip (45113 Bytes | downloaded 93 times)
Report
Re: using c++ DLL with template class Posted by NoiK00 on 9 Aug 2010 at 5:04 AM
i think i have narrowed down the question:

When using the function:
std::string CTPGKIXOption3::MakeKIX(const std::string& strAddress, const std::string& strPostalCode)

and i use REAL std::strings it all works fine. But when i use a String^ converted to a std::string it gives a debug error. the error states string Subscript out of range.

the function used for conversion is:
	void MarshalString( String ^ s, std::string& os ) {
		using namespace Runtime::InteropServices;
		const char* chars = 
			(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
		os = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));
	}

What is going wrong in the conversion?

TIA



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.