[OT newbie C++] How do I cast a vector to a HANDLE

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: Hacker, petermad, Stefan2, white

Post Reply
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

[OT newbie C++] How do I cast a vector to a HANDLE

Post by *ctiberg »

I'm looking into C++ because my Delphi FS plugins freeze TC, and I would like to do one specific thing :)

I'm having trouble with the return type of FsFindFirst - internally I decided to use a vector of WIN32_FINDDATAA, but how do I get that into the HANDLE return value? In Delphi I would just do THandle(integer(MyList))...

In addition - does anyone of you know of a C++ forum, preferrably with one or more TC plugin authors active?
Best regards,
Christian Tiberg
User avatar
m^2
Power Member
Power Member
Posts: 1413
Joined: 2006-07-12, 10:02 UTC
Location: Poland
Contact:

Post by *m^2 »

Code: Select all

static_cast<HANDLE>(vec&)
should be ok.
C style

Code: Select all

(HANDLE)vec*
is.
Last edited by m^2 on 2008-04-10, 08:01 UTC, edited 1 time in total.
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

Thanks a lot, that makes life easier for me. No tips on forums you or anyone else here is active on?
Best regards,
Christian Tiberg
User avatar
m^2
Power Member
Power Member
Posts: 1413
Joined: 2006-07-12, 10:02 UTC
Location: Poland
Contact:

Post by *m^2 »

Nope, I'm not a frequent visitor of any C/C++ forum.
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6970
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Post by *Horst.Epp »

ctiberg wrote:Thanks a lot, that makes life easier for me. No tips on forums you or anyone else here is active on?
Try www.codeproject.com

This site is sometime very slow but has a lot of examples and articles.
CoolWater
Power Member
Power Member
Posts: 744
Joined: 2003-03-27, 16:33 UTC

Re: [OT newbie C++] How do I cast a vector to a HANDLE

Post by *CoolWater »

ctiberg wrote:I'm looking into C++ because my Delphi FS plugins freeze TC, and I would like to do one specific thing :)

I'm having trouble with the return type of FsFindFirst - internally I decided to use a vector of WIN32_FINDDATAA, but how do I get that into the HANDLE return value? In Delphi I would just do THandle(integer(MyList))...

In addition - does anyone of you know of a C++ forum, preferrably with one or more TC plugin authors active?
Are talking about an STL vector (std::vector)? Then you should rather cast an specific item instead of casting the entire vector... i.e.

HANDLE h = (HANDLE) vector.at(0);

Regards,
CoolWater
poiuytr
Senior Member
Senior Member
Posts: 243
Joined: 2003-02-23, 17:33 UTC

Re: [OT newbie C++] How do I cast a vector to a HANDLE

Post by *poiuytr »

CoolWater wrote:Are talking about an STL vector (std::vector)? Then you should rather cast an specific item instead of casting the entire vector... i.e.

HANDLE h = (HANDLE) vector.at(0);
If a topicstarter is talking about something like FsFindFirst() then he really needs a pointer to the whole vector.
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

Yep, I most definitely did need a pointer to the whole vector. Some problems remain, though - I'll continue on C++ forums instead of here.
Best regards,
Christian Tiberg
CoolWater
Power Member
Power Member
Posts: 744
Joined: 2003-03-27, 16:33 UTC

Post by *CoolWater »

ctiberg wrote:Yep, I most definitely did need a pointer to the whole vector. Some problems remain, though - I'll continue on C++ forums instead of here.
What kind of problems? :) I think Christian doesn't mind you to post here since in this forum are quite alot advanced c++ programmers (like me) ;)

Regards,
CoolWater
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

OK, here goes. Here's a type I've declared:

typedef vector<WIN32_FIND_DATAA> WIN32_FIND_DATAA_VECTOR;


I've received enough help to feel that I'll be able to do the FsFindFirst function, at least syntactically :) Now the problem is in FsFindNext, where I've got the following:

WIN32_FIND_DATAA_VECTOR* vec = (WIN32_FIND_DATAA_VECTOR*) Hdl;

That does seem to get me the vector that I returned from FsFindFirst. But then I can't seem to get at an element, or to remove it from the vector. Here's what I have:

FindData = vec[0];
vec->erase(0, 0);

None of the statements compile, and I can't get my head around why they don't :)

The next problem comes in FsFindClose, how do I get rid of the vector? Assuming that I use the same stuff as in FsFindNext (above), can I use

delete vec

to get rid of it?

I've uploaded the whole project to http://www.drop.io/ctiberg - please take a look at it, and if you do please re-upload it with another name :)

I'll worry about the other parts of the plugin later, right now I'm busy eating a C++ book :)

One other thing - how do I get "Microsoft Visual C++ 2008 Express Edition" to name the resulting file .wfx instead of .dll?
Best regards,
Christian Tiberg
CoolWater
Power Member
Power Member
Posts: 744
Joined: 2003-03-27, 16:33 UTC

Post by *CoolWater »

ctiberg wrote:OK, here goes. Here's a type I've declared:

typedef vector<WIN32_FIND_DATAA> WIN32_FIND_DATAA_VECTOR;


I've received enough help to feel that I'll be able to do the FsFindFirst function, at least syntactically :) Now the problem is in FsFindNext, where I've got the following:

WIN32_FIND_DATAA_VECTOR* vec = (WIN32_FIND_DATAA_VECTOR*) Hdl;

That does seem to get me the vector that I returned from FsFindFirst. But then I can't seem to get at an element, or to remove it from the vector. Here's what I have:

FindData = vec[0];
vec->erase(0, 0);

None of the statements compile, and I can't get my head around why they don't :)

The next problem comes in FsFindClose, how do I get rid of the vector? Assuming that I use the same stuff as in FsFindNext (above), can I use

delete vec

to get rid of it?

I've uploaded the whole project to http://www.drop.io/ctiberg - please take a look at it, and if you do please re-upload it with another name :)

I'll worry about the other parts of the plugin later, right now I'm busy eating a C++ book :)

One other thing - how do I get "Microsoft Visual C++ 2008 Express Edition" to name the resulting file .wfx instead of .dll?
Ok, several things which I would do in another way:

instead of:
typedef vector<WIN32_FIND_DATAA> WIN32_FIND_DATAA_VECTOR;

use:
typedef vector<WIN32_FIND_DATAA*> WIN32_FIND_DATAA_VECTOR;

Each vector element must be a pointer! So in your enumPodcasts function you should do something like that:

Code: Select all

WIN32_FIND_DATAA_VECTOR enumPodcasts(void)
{
	WIN32_FIND_DATAA_VECTOR vec;
	char buf[2048];
	int bufSize = 2048;
	char* str;
	GetPrivateProfileSectionA("PodCatcherFS", buf, bufSize, iniName);
	str = buf;
	while (str)
	{
		strcpy_s(en.cFileName, MAX_PATH, str);
		WIN32_FIND_DATAA en = new WIN32_FIND_DATAA();
		en.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
		en.nFileSizeHigh = 0;
		en.nFileSizeLow = 0;
		vec.push_back(en);
		str += strlen(str);
	}
	return vec;
	//return static_cast<HANDLE>(&vec);
}
Then, in FsFindClose, iterate through all vector elements and delete each of it, i.e. delete (WIN32_FIND_DATAA*) vec[0];, delete (WIN32_FIND_DATAA*) vec[1]; and so on.

But, what i personally would do... I would make your vector to a global variable instead of passing it between functions, i.e. define it just like int myPluginNr;...

typedef vector<WIN32_FIND_DATAA> WIN32_FIND_DATAA_VECTOR;
WIN32_FIND_DATAA_VECTOR g_vec;

And then just work with g_vec. g_vec itself cannot be deleted since it is a stack variable. So just delete all elements/pointer and you're done :)

Concerning the wfx/dll file. In the project properties, there should be an entry for the output filename, i.e. Debug\my.dll There, you can replace dll with wfx.

HTH a bit ;)

Regards,
CoolWater
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

I have a severe allergy to globals. They always cause problems, and should be avoided if at all possible. The only reason for keeping myPluginNr et.al. global is because they don't change. gVec would.
Best regards,
Christian Tiberg
CoolWater
Power Member
Power Member
Posts: 744
Joined: 2003-03-27, 16:33 UTC

Post by *CoolWater »

ctiberg wrote:I have a severe allergy to globals. They always cause problems, and should be avoided if at all possible. The only reason for keeping myPluginNr et.al. global is because they don't change. gVec would.
Hmm, I never had troubles with globals... Which problems did you encounter using globals? Nevertheless, you can of course pass the vector as parameter. But therefore you should consider my hints, too. ;)

Regards,
CoolWater
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

CoolWater wrote:Hmm, I never had troubles with globals... Which problems did you encounter using globals? Nevertheless, you can of course pass the vector as parameter. But therefore you should consider my hints, too. ;)
Yeah, sure I'll consider your hints :)

The thing about not using globals is written with huge letters in every programming book, or variations thereof. Globals are considered a bad thing because (and these two are the ones I can remember right now):

1) A function that modifies a global variable has invisible side effects. Calling it without being aware of that will give you severe problems.
2) Global variables don't work well with threads. Having multiple threads use global variables requires the use of critical sections or similar (perhaps hidden by the compiler by some trick, but still there) to regulate access, so that they don't mess each other up.
Best regards,
Christian Tiberg
User avatar
ctiberg
Member
Member
Posts: 194
Joined: 2003-10-24, 14:10 UTC
Location: Kristianstad, Sweden

Post by *ctiberg »

I'm back again, with a couple of questions :)

I've found code to use urlmon to download a file, like so:

#include <urlmon.h>
if (URLDownloadToFileA(0, buf, dst, 0, 0))
{
}

But this gives me an unresolved external, and while I do realize what's causing it, I'm not able to solve it. Where do I need to tell my project to link to urlmon(.lib)?

Another thing is loading an RSS feed and parsing it. I found code online to load and parse XML files, but it was for .NET. So does anyone have some code for regular C++? A bonus is of course if it's actually code to parse an RSS feed, but any XML parsing examples are appreciated.

For FsGetFile, I'll also need a better version of the call to URLDownloadToFileA above - it'll need to have a callback so that I can give feedback to TC. Any pointers to a useful example?
Best regards,
Christian Tiberg
Post Reply