|
Hi everybody. I have a question regarding adding a method to premade code using visual c++. Here is the method:
void GetAllFiles( string sPath )
{
WIN32_FIND_DATA FindFileData;
string sTmpPath = sPath;
sTmpPath += "\\*.*";
HANDLE hFind = FindFirstFile( sTmpPath.c_str(), &FindFileData );
if ( hFind == INVALID_HANDLE_VALUE )
return;
else {
do {
if ( ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
if ( strcmp(".", FindFileData.cFileName ) && strcmp("..", FindFileData.cFileName) ) {
sTmpPath = sPath;
sTmpPath += "\\";
sTmpPath += FindFileData.cFileName;
GetAllFiles( sTmpPath.c_str() );
}
}
else {
sTmpPath = sPath;
sTmpPath += "\\";
sTmpPath += FindFileData.cFileName;
cout << sTmpPath << endl;
}
} while ( FindNextFile( hFind, &FindFileData) != 0 );
FindClose( hFind );
}
return;
}
When I add it to the bottom of my form.h (below) file it comes up with these errors (bottom):
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::ListBox^ list;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->list = (gcnew System::Windows::Forms::ListBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
this->textBox1->Location = System::Drawing::Point(97, 23);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(518, 20);
this->textBox1->TabIndex = 0;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 26);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(79, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"Libray Location";
this->list->FormattingEnabled = true;
this->list->Location = System::Drawing::Point(15, 63);
this->list->Name = L"list";
this->list->Size = System::Drawing::Size(757, 238);
this->list->TabIndex = 2;
this->button1->Location = System::Drawing::Point(697, 337);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 3;
this->button1->Text = L"Play";
this->button1->UseVisualStyleBackColor = true;
this->button2->Location = System::Drawing::Point(616, 337);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 4;
this->button2->Text = L"Refresh";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(784, 372);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->list);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
}
void GetAllFiles( string sPath )
{
WIN32_FIND_DATA FindFileData;
string sTmpPath = sPath;
sTmpPath += "\\*.*";
HANDLE hFind = FindFirstFile( sTmpPath.c_str(), &FindFileData );
if ( hFind == INVALID_HANDLE_VALUE )
return;
else {
do {
if ( ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
if ( strcmp(".", FindFileData.cFileName ) && strcmp("..", FindFileData.cFileName) ) {
sTmpPath = sPath;
sTmpPath += "\\";
sTmpPath += FindFileData.cFileName;
GetAllFiles( sTmpPath.c_str() );
}
}
else {
sTmpPath = sPath;
sTmpPath += "\\";
sTmpPath += FindFileData.cFileName;
cout << sTmpPath << endl;
}
} while ( FindNextFile( hFind, &FindFileData) != 0 );
FindClose( hFind );
}
return;
}
};
1>------ Build started: Project: TestForms, Configuration: Debug Win32 ------
1> TestForms.cpp
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(134): error C2061: syntax error : identifier 'string'
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(136): error C2065: 'WIN32_FIND_DATA' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(136): error C2146: syntax error : missing ';' before identifier 'FindFileData'
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(136): error C2065: 'FindFileData' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(137): error C2065: 'string' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(137): error C2146: syntax error : missing ';' before identifier 'sTmpPath'
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(137): error C2065: 'sTmpPath' : undeclared identifier
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(137): error C2065: 'sPath' : undeclared identifier
|
|
|
|
|
try
#include "windows.h"
#include <string>
at the top of the file
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
 It got rid of most of the errors I was looking at but now I have some more.
1>------ Build started: Project: TestForms, Configuration: Debug Win32 ------
1> TestForms.cpp
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(149): error C2664: 'strcmp' : cannot convert parameter 2 from 'WCHAR [260]' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(149): error C2664: 'strcmp' : cannot convert parameter 2 from 'WCHAR [260]' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(152): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'WCHAR [260]' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(777): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(782): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(787): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, WCHAR [260])'
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(160): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'WCHAR [260]' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(777): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(782): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(787): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator +=(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, WCHAR [260])'
1>c:\users\steve\documents\visual studio 2010\projects\testforms\testforms\Form1.h(161): error C2065: 'cout' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any idea how to fix this?
|
|
|
|
|
Your project properties are probably set to use Unicode instead of ANSI for all your strings.
Either change the project properties to use ANSI, or use the Unicode version of the strcmp function.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thank you very much you solved all my problems. I can't thank you enough.
|
|
|
|
|
Also check to make sure that your project is set to use native C++ code in your CLI project. Not sure if that will help or not.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
modified 3-Apr-12 21:57pm.
|
|
|
|
|
I do not fully understand what you mean. Is that an option in Visual Studio? I also came up with another question. How can I convert my System::String^ (in button2_click()) to std::string (in GetAllFiles(string))?
|
|
|
|
|
Member 7997264 wrote: I do not fully understand what you mean
You are trying to use native C++ code in your managed C++ project. See this article[^]
Member 7997264 wrote: System::String^ (in button2_click()) to std::string
See this article[^]
You might get a better response if you asked your questions in the managed c++ forum, here[^]. This forum is for unmanaged C++.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Why does the follwing throw an error @ delete []pBuffer;
char *pBuffer = new char[1024];
CString csBuf = (CString)pMember->m_member_label;
pBuffer = csBuf.GetBuffer(sizeof(pBuffer));
xtext=(GLfloat)(((pMember->m_xs)+(pMember->m_xe))/2.0);
ytext=(GLfloat)(((pMember->m_ys)+(pMember->m_ye))/2.0);
ztext=(GLfloat)(((pMember->m_zs)+(pMember->m_ze))/2.0);
m_bmf->DrawStringAt((GLfloat)(xtext), (GLfloat)(ytext+5), (GLfloat)(ztext), pBuffer);
delete []pBuffer;
|
|
|
|
|
ARRRRGH. You overwrite pBuffer with the result of CString's GetBuffer(), which gives you a pointer to a buffer you don't own. Then you try to delete it. BOOM.
You cannot delete the pointer returned by GetBuffer(), that memory belongs to the CString Object and it will delete it when the object is deleted or goes out of scope.
Plus, you've lost the pointer to the char[1024] that you did allocate with new, leaving a memory leak for that memory.
|
|
|
|
|
Thanks.
So, I'm an idiot. What do I do?
I did this, and it at least ran:
pBuffer=NULL;
delete []pBuffer;
|
|
|
|
|
Setting pBuffer = NULL and then deleting it is a big NOP, a no-operation. You might as well just deleted the "delete" statement.
Instead, if your sole purpose in doing the "GetBuffer" is to pass a string to DrawStringAt(), then you don't need to "new" or "delete" anything in that program. All you need is:
CString csBuf = (CString)pMember->m_member_label;
char *pBuffer = csBuf.GetBuffer(1);
xtext=(GLfloat)(((pMember->m_xs)+(pMember->m_xe))/2.0);
ytext=(GLfloat)(((pMember->m_ys)+(pMember->m_ye))/2.0);
ztext=(GLfloat)(((pMember->m_zs)+(pMember->m_ze))/2.0);
m_bmf->DrawStringAt((GLfloat)(xtext), (GLfloat)(ytext+5), (GLfloat)(ztext), pBuffer);
Or better yet, avoid the GetBuffer() entirely and pass "csBuf" to DrawStringAt() and let the compiler convert from CString to char * as needed.
|
|
|
|
|
This did it: (you approve?)
CString csBuf = (CString)pMember->m_member_label;
char *pBuffer = csBuf.GetBuffer(sizeof(pBuffer));
xtext=(GLfloat)(((pMember->m_xs)+(pMember->m_xe))/2.0);
ytext=(GLfloat)(((pMember->m_ys)+(pMember->m_ye))/2.0);
ztext=(GLfloat)(((pMember->m_zs)+(pMember->m_ze))/2.0);
m_bmf->DrawStringAt((GLfloat)(xtext), (GLfloat)(ytext+5), (GLfloat)(ztext), pBuffer);
pBuffer=NULL;
delete []pBuffer;
|
|
|
|
|
Nope, see my response to the earlier post
|
|
|
|
|
You really need to study this[^] carefully. You also need to learn about the sizeof [^], new [^] and delete [^] operators.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
why pBuffer=NULL;
and then delete []pBuffer;
delete what? Only a invalid pointer
|
|
|
|
|
What in m_member_label is a CString pointer or not?
Or you should not do any thing, CString class local instance will destroy at the end of the fun automatically except a pointer from other place?
|
|
|
|
|
My application is working fine on 32 bit, I am trying to run in 64 bit mfc application, GUI is launching, the model is also loaded but the moment I press left right or middle mouse it crashes. what all i need to change to run successfully in 64 bit machine.
The error I found in OnMouseActive() function, do i need to implement this function ? Pls help me. Thanks a lot in advance
|
|
|
|
|
Hi,
Is there any machenism to check the socket status (alive or dead )?
|
|
|
|
|
What do you mean by "alive" and "dead"?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
If there is any doubt about the condition of a socket, it's best to simply Close() it and open a new one.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
john5632 wrote: Is there any machenism to check the socket status (alive or dead )?
Send a request and get a response.
|
|
|
|
|
Hi all,
I am trying to map logical drive to physical drive i.e. when i pass D: then it should get that which Physical drive does D: belongs to.
I am using IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS for achieving my goal. My problem is it is returning error code 87 INVALID_PARAMETER.
I am not getting that what i am doing wrong
here is my code
CString str = _T("\\\\.\\\\D:");
HANDLE h = CreateFile(str, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(INVALID_HANDLE_VALUE != h)
{
VOLUME_DISK_EXTENTS sd;
DWORD dwRet;
ret = DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, &sd, sizeof(VOLUME_DISK_EXTENTS), &dwRet, NULL);
if(ret != 0)
{
AfxMessageBox(_T("Success"));
}
else
{
DWORD d;
d = GetLastError();
}
}
|
|
|
|
|
Use this (single backslash in front of device name / drive letter)
CString str = _T("\\\\.\\D:");
|
|
|
|
|
Your pathname is incorrect, it contains two backslash characters after the dot rather than one, as described here[^]. You should check the return value from your CreateFile() call before proceeding.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|