|
Actually, you can use a managed container if you use "IntPtr". The following example is using C++/CLI:
using namespace System;
using namespace System::Collections::Generic;
int main(array<System::String ^> ^args)
{
int* ptr = nullptr;
List<IntPtr>^ list = gcnew List<IntPtr>;
try
{
for (int i = 0; i < 10; ++i)
{
ptr = new int(i);
list->Add(IntPtr(ptr));
}
for each (IntPtr iptr in list)
{
ptr = static_cast<int*>(iptr.ToPointer());
Console::WriteLine(*ptr);
}
}
catch (Exception^ e)
{
Console::WriteLine(e->Message);
}
finally
{
for each (IntPtr iptr in list)
{
delete static_cast<int*>(iptr.ToPointer());
}
}
return 0;
}
|
|
|
|
|
hey, if somebody has or know how to make a program that reads all files from a CD, would be great. it would be a great help... thanks
|
|
|
|
|
Hi,
How do you want to read them ?
1) Copy them
2) Rip them into an ISO image
3) etc...
regards,
Darka [ Xanya]
"I am not a slave to a god that doesn't exist."
|
|
|
|
|
I have a managed c++ class (TestClass) that is derived from a managed C++ template class (TestBaseTemplate).
In my c# client I create a new TestClass object. I can access properties defined in the template base class but not methods
Here's an example
c++ code
namespace DotNetTest
{
template<typename T>
public ref class TestBaseTemplate abstract
{
public:
TestBaseTemplate(){}
~TestBaseTemplate(){}
System::String ^ GetAString()
{
return gcnew System::String("GetAStringMethod");
}
property System::String ^ AString
{
System::String ^ get()
{
return gcnew System::String("StringProperty");
}
}
};
public ref class TestClass : public TestBaseTemplate<System::Int32>
{
public:
TestClass(){}
~TestClass(){}
};
}
c# code
DotNetTest.TestClass test = new DotNetTest.TestClass();
string s = test.AString;
test.GetAString();
The last line produces the following compiler error
error CS0117: 'DotNetTest.TestClass' does not contain a definition for 'GetAString'
Why does the call to the property work and not the call to the method?
|
|
|
|
|
I believe the property actually redefines the method.
the compiler implements properties as method cals getProertyName() and putProretyname().
your name for the method GetAString vollides with the implementation name of the property getter for AString - GetAString
|
|
|
|
|
Thanks for your response. I look at it in the disassembler and the property was called get_AString. Turns out I needed to do a "clean all" and rebuild. I had previously tried a "rebuild entire solution"
|
|
|
|
|
Hello,
Sorry for the really, really basic post - but I've encountered a problem I can't seem to figure out.
I'm trying to compile a command line program in VC++ Express 2005. It consists of three files:
Main.cpp, Node.h, Node.cpp
<br />
#include "Node.h"<br />
<br />
void main()<br />
{<br />
Node<int> myNode;<br />
}<br />
<br />
#ifndef _NODE_H_<br />
#define _NODE_H_<br />
<br />
template <typename T><br />
class Node<br />
{<br />
public:<br />
Node();<br />
Node(T initialData);<br />
<br />
void setData(T newData);<br />
T getData();<br />
private:<br />
T data;<br />
};<br />
<br />
#endif<br />
<br />
#include "Node.h"<br />
<br />
template <typename T><br />
Node<T>::Node()<br />
{<br />
}<br />
<br />
template <typename T><br />
Node<T>::Node(T initialData) : data(initialData)<br />
{<br />
}<br />
<br />
template <typename T> <br />
void Node<T>::setData(T newData)<br />
{<br />
data = newData;<br />
}<br />
<br />
template <typename T><br />
T Node<T>::getData()<br />
{<br />
return data;<br />
}<br />
This is my first time working with templates. I've striped everything WAY down so that it's just the basics. The error message is very non-descriptive:
Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::Node<int>(void)" (??0?$Node@H@@QAE@XZ) referenced in function _main
I really hope someone can shed some light on this for me. I'm just trying to get used to working with templates, but I can't move past this point without figuring out what is causing the problem. Thanks.
http://www.atraeyu.org/
|
|
|
|
|
Welcome to CodeProject. There is a forum specific for STL/ATL questions and this question belongs in there. Also please notice that the '>' and '<' characters are part of HTML syntax and must be escaped to show up in your posts. You should read some of the instructions for posting code in the forums.
led mike
|
|
|
|
|
This isn't a managed C++ question, please use the Visual C++ forum in the future.
Check out Templates in the C++ FAQ Lite[^] as it covers a lot of template topics and answers your question.
|
|
|
|
|
First of all, I agree with the previous reply post. This is the C++/CLI (Managed C++) forum.
Secondly, when you are working with template classes, all your code need to be placed in the header file.
George
|
|
|
|
|
Thank you, and sorry for posting in the wrong forum. I found my answer - my textbook glossed over the fact that templates actually generate the classes/functions and therefore require all the code to be in the header file.
The C++ Lite FAQ did a great job of explaining the answer and possible solutions. Thanks again, and sorry.
http://www.atraeyu.org/
|
|
|
|
|
Hi All,
This works fine in debug mode, but in release mode I am getting errors thrown when passing std::wstring from managed to un-managed. I narrowed it down to one line...
std::wstring name2=L"test";
which in the debugger in debug build says {"test"}, but in release mode it says {"st"}.
To add to this, when I try and call my unmanaged function with...
m_node=new NW::Node(L"test");
it works fine in debug mode, but in release mode it blows up with unknown software exception.
If I change from Multithreaded DLL to Multithreaded debug dll in the c++ code generation options, it works. If I change back... it stops working again.
I thought it was maybe because I was passing std::wstring and that the implementation may somehow be different between the unmanaged library and the managed assembly, but I changed it to wchar_t and I still get the same problem.
I am very confused. Help!
Thanks
Rael
|
|
|
|
|
do you have UNICODE set in both DEBUG and RELEASE builds?
led mike
|
|
|
|
|
Yes, I checked that for all modules. I also checked to make sure they all have 'treat wchar_t as built in type' set to yes.
Another interesting thing. If I change from std::wstring to wchar_t* it still fails, but if I then turn off 'whole program optimization' then wchar_t* starts working, but std::wstring fails.
[Edit] Actually I tell a lie... it seems intermittent.
FYI, I have my unmanaged code compiled as a .lib using the /MD option, and my managed code is a DLL linking in the .lib also compiled with the /MD option.
Do you think this could be to do with the owner of the memory? Is it safe to pass pointers to memory allocated in managed code over to un-managed code?
Really scratching my head over this one.
Thanks
-- modified at 12:09 Monday 23rd October, 2006
|
|
|
|
|
Raeldor_ wrote: Is it safe to pass pointers to memory allocated in managed code over to un-managed code?
Memory should be marshalled from native to managed and managed to native. But your post does not indicate that is happening.
led mike
|
|
|
|
|
I am doing the following where in_name is System::String and name is std::wstring, and the node parameter is wchar_t* (used to be std::wstring, but that didn't work either)...
MNW::MarshalString(in_name, name);
m_node=new NW::Node(name.c_str());
Where NW::Node is my unmanaged class. MarshalString is...
void MarshalString(System::String^ s, std::wstring& os)
{
const wchar_t* chars =
(const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
Do I need to do more than this?
[EDIT]
having said that, even this...
pin_ptr<const wchar_t> str=L"test";
m_node=new NW::Node(str);
Blows an error. UNLESS I compile with the debug DLL. :S
-- modified at 12:47 Monday 23rd October, 2006
|
|
|
|
|
Not sure this will help but I would try:
IntPtr iPtr = Marshal::StringToHGlobalUni(s);
const wchar_t* chars = (const wchar_t*)iPtr.ToPointer();
os = chars;
Marshal::FreeHGlobal(iPtr);
MODIFIED: Keep in mind I am really confused since your initial post makes no mention of managed memory and does not seem to match at all what you have put in your last post.
led mike
|
|
|
|
|
Ugh, please excuse me... I am a moron. I copy+pasted the additional library directories from the debug to the release version, so it's picking up the debug versions of the libraries.
Just out of interest, would you tend to pass strings as parameters by std::wstring or wchar_t*? Is there a performance hit either way?
Thanks for your patience!
|
|
|
|
|
Raeldor_ wrote: would you tend to pass strings as parameters by std::wstring or wchar_t*?
Well for an "output" parameter wchar_t* is not going to work. For input parameter you could overload:
void MarshalString(std::wstring& is)
{
MarshalString( is.c_str());
}
void MarshalString(wchar_t* pis)
{
}
NOTE: std::wstring& is a "reference" which does not have any performance penalties compared with wchar_t*.... however std::wstring will copy the string data onto the stack.
void MarshalString(std::wstring& is)
is not equal to
void MarshalString(std::wstring is)
led mike
|
|
|
|
|
I always initialize variables by habit from C/C++ programming.
So in managed C++ I'll do this:
Int32 MyInt32 = 0;
I was told this is redundant. Is that true? And if so I would sure like to know where
in the documentation it states that value type are initialized to 0 - for peace of mind.
Until I see it I'll live with redundancy
Mark
|
|
|
|
|
Mark Salsbery wrote: I was told this is redundant.
While technically it is true that is not a reason to discontinue a "good practice" IMHO.
led mike
|
|
|
|
|
Thanks led mike.
Do you happen to know where it's documented? I can't find it anywhere!
Mark
|
|
|
|
|
Probably arises from this: Do not initialize unnecessarily[^]
However, it also explicitly states: "This rule ignores Managed C++ assemblies."
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
How can I get the DNS IP address that the local client is using?
Like to find a way using c++ and not MFC
thanX
|
|
|
|
|
Hi,
I am trying to wrap my C++ objects in a managed wrapper. I have...
public ref class MCamera
{
private:
NW::Camera* m_camera;
public:
MCamera(MNW::Vector3 in_source, MNW::Vector3 in_target, float in_near, float in_far, int in_outputWidth, int in_outputHeight);
~MCamera();
IntPtr GetCameraPtr();
};
and I'm putting a breakpoint on the code for the destructor but it never gets called. I've enabled unmanaged debugging and other breakpoints work, but it seems this never gets called. Does anyone know why the destructor is not getting called, even when the application is closed?
Thanks!
|
|
|
|