|
I posted in this forum because i'm using a mix of managed and not managed c++, but it's true that my question wasn't about managed, sorry.
And I also tought that BYTE was the same thing as unsigned char, but since i can't put a byte into an unsigned char like this: unsignedCharVar = BYTEVar , I was having doubts. So my question is how can I put a byte var into an unsigned char one(and no I can't use the byte directly, I need to test something)
Here is exactly what I tried:
UINT iFileLength = fFile.GetLength();
LPBYTE pByteBuff = new BYTE[iFileLength];
unsigned char cByteBuff[1];
UINT iCompteur = 0;
cByteBuff = pByteBuff[iCompteur-1];
|
|
|
|
|
samkook wrote: I posted in this forum because i'm using a mix of managed and not managed c++, but it's true that my question wasn't about managed, sorry.
That's cool, if you're using managed, then the only issue is, more people would see if you posted in the other forum
samkook wrote: but since i can't put a byte into an unsigned char like this: unsignedCharVar = BYTEVar
A BYTE is a typedef, if you need to cast it, I'd be really surprised, but if you did, casting it would totally work.
Your problem is completely different. You don't have an unsigned char, you ( for reasons I do not understand ) have an array of unsigned chars, which has a length of one. Why do you need an array ?
You're also setting iCompteur to 0, and trying to go 1 minus that for your index. This will, of course, blow up.
Your problem has nothing to do with conversion between two types ( which are the same ). Don't try to index outside the array (0-1 is outside the array ) and don't try to set an array to equal a value. Set a value IN the array on the left, as you are on the right.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Humm, Sorry about that, I'm not going out of the array, I only didn't put the code where I increment iCompteur. And I was using an array because a function needed it and if I only gave it an unsigned char, it was giving me an error(another case of me doing weird things because I can't find the right conversion).
Also, How do I set the value IN the array. I'm not sure how to do it(forget this line, it was a stupid question).
I just tested what I wanted to test an at my big surprise I was right, it was the problem(now to explain it). I'm reading text from a binary file, and displaying it into an editbox. Before displaying it, I'm putting each bytes into a CString and then I display it. If i Do this: csSpeech += (char *)cByteBuff; it's working and if I do this: csSpeech += (char *)pByteBuff[iCompteur-1]; it's crashing and finally if I do this: csSpeech += (char *)&pByteBuff[iCompteur-1]; all the sentence is reapeated with dropping a letter each time(for exemple if the sentence is abc it will be abcbcc). Any idea of what i'm doing wrong. BTW, before this i'm transfering the value of the byte array(the big one) into the small one.
|
|
|
|
|
samkook wrote: csSpeech += (char *)pByteBuff[iCompteur-1];
Of course this will crash, the index gives you a char, and you tell the computer to look at the address that number represents for a string.
samkook wrote: csSpeech += (char *)&pByteBuff[iCompteur-1];
Why would you do this ? You're getting the address with &, why not just cast to char ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
|
A few questions from a long time programmer but .NET newbie:
I seem to be having an awful lot of trouble getting a textbox set up that will properly use the userpaint flag. Whenever I set it, I can then override the onPaint and onBackgroundPaint functions and have them do their own thing (such as filling the background with a gradient, which is my immediate goal) but it seems that there is still a lot of things that the textbox control simply draws on it's own without any input from me: Cursors, borders, selected text, etc.
Is there anything further I need to override in order to completely control the control painting, or is this simply one of those things that are *shudder* hardcoded into the system?
Some sample code, for kicks:
<br />
public ref class MyTextBox : public System::Windows::Forms::TextBox<br />
{<br />
public:<br />
MyTextBox (void)<br />
{<br />
this->SetStyle(ControlStyles::UserPaint | ControlStyles::AllPaintingInWmPaint, true);<br />
this->SetStyle(ControlStyles::SupportsTransparentBackColor, true);<br />
this->SetStyle(ControlStyles::OptimizedDoubleBuffer, true);<br />
}<br />
<br />
protected:<br />
~MyTextBox ()<br />
{<br />
if (components) { delete components; }<br />
}<br />
<br />
private:<br />
System::ComponentModel::Container ^components;<br />
<br />
protected:<br />
virtual void OnPaintBackground(PaintEventArgs^ e) override {<br />
Drawing::Brush^ backBrush = gcnew Drawing2D::LinearGradientBrush(<br />
this->ClientRectangle,<br />
Color::AliceBlue, <br />
Color::LightBlue,<br />
Drawing2D::LinearGradientMode::Vertical);<br />
<br />
e->Graphics->FillRectangle(backBrush, this->ClientRectangle);<br />
<br />
delete backBrush;<br />
}<br />
<br />
virtual void OnPaint(PaintEventArgs^ e) override {<br />
System::Drawing::Brush^ tBrush = SystemBrushes::ControlText;<br />
<br />
StringFormat^ tFormat = gcnew StringFormat();<br />
tFormat->Alignment = StringAlignment::Near;<br />
tFormat->LineAlignment = StringAlignment::Near;<br />
tFormat->Trimming = StringTrimming::None;<br />
<br />
e->Graphics->DrawString(this->Text, this->Font, tBrush, this->ClientRectangle, tFormat);<br />
}<br />
A few other questions, if anyone cares to answer:
-Is there a CLI equivalent to the C# "base" keyword? I have plenty of times that I'd like to call something like base.OnPaint(), but there doesn't seem to be a resonable way of doing that in C++/CLI. (For example, in my code above TextBox::OnPaint(e) would NOT work, it would simply call my overridden version.)
-If I declare a virtual function as "new" (as opposed to override), how in the world to I call it? It always seems to default to the function for the class I inherited it from.
-Is there a way that you can just override OnPaintBackground without overriding OnPaint? I thought you could just leave out your own OnPaint definition, but this doesn't appear to work so well in practice (for instance: all my fonts become an ugly bold "system" font)
Thanks in advance for all your help!
|
|
|
|
|
Dear All,
I am not sure whether I should post this question in this forum.
If not please let me know the correct forum to post the question.
I am using CPPUnit::XmlOutputter to write the unit test results to an XML file.
The program works as expected and I can see the test results in the xml file.
But I also want to add intermediate results and user defined messages in the same xml result file.
How do I do that?
Thanks a ton,
Arti gujare
|
|
|
|
|
Hello:
I am developping a MC++ dll that reuses tons of code that was made in unmanaged C++. What I am trying to do now is to store unmanaged objects in a managed collection.
Let's suppose that I have an unmanaged class (from an unmanaged c++ dll)
CMyUnmanaged pUnmanaged* = new CMyUnmanaged;
And, on the other hand I have a .net collection, a hashlist in this case:
System::Collections::Hashlist* hl = new System::Collections::Hashlist;
What I would like to achive is something like this:
hl->Add(pUnmanaged->Name, pUnmanaged);
CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name];
But as .net collections can only store System::Objects, that is managed objects, I cannot do it the straight way.
How can I convert my unmanaged pointers in order to store them in the hashlist?
Is there a better way to do this?
Thank you very much!
|
|
|
|
|
Jose M Castellanos wrote: And, on the other hand I have a .net collection, a hashlist in this case:
System::Collections::Hashlist* hl = new System::Collections::Hashlist;
What I would like to achive is something like this:
hl->Add(pUnmanaged->Name, pUnmanaged);
CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name];
Im not sure if you can do this but even if you could you would not be able to use the collection outside of managed c++ as it contains un-managed classes. Why not just use an STL container?
|
|
|
|
|
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
|
|
|
|
|