Fog Creek Software
Discussion Board




VB6 & Pointers

Theoretically, is true that with StrPtr(), VarPtr(), ObjPtr() and the CopyMemory API call, one can do everything one does in C ?

Regards

Kaushik Janardhanan

KayJay
Monday, March 29, 2004

How would you deal with a CallBack function?

(I.e., a function that you call from VB that will "notify" VB when some action is completed).

I don't know much about pointers, but I thought that was a significant limitation of VB 6.

Mr. Analogy
Monday, March 29, 2004

The AddressOf operator? Unless calling back to functions only in a module is considered a drawback.

KayJay
Monday, March 29, 2004

Callback.

Well you have late binding in VB6. So pass the function name as a string! then use

obj.strMethodName

Savage
Monday, March 29, 2004

"How would you deal with a CallBack function?"

From VB - Implement an interface, like you ought to in any other object oriented language.

From API (or any DLL implemented in C) use the AddressOf operator to get pointer to standalone function.

Next?

Craig
Monday, March 29, 2004

What exactly do you mean by "can you do everything you can do in C"?  If you mean, does VB have the ability to simulate every operation in C, I don't really know.  If you are asking whether any C program can instead be written in VB, then the answer is that they are both Turing complete languages and thus can each fully express all computable programs.  They may just do so in different ways

MikeMcNertney
Monday, March 29, 2004


I break down 'stuff you can do with pointers in VB' into the following broad categories:

- Manipulating binary data structures.  You can do lots of fiddling with global memory blocks and arrays of bytes.

- Calling APIs that take structures with pointers as arguments.  You have to know exactly how VB implements strings and structures.  Once you've learned the basics, you should be able to figure out any API function and translate it into a VB equivalent.  Hint - you sometimes have to pack and unpack structures yourself because VB doesn't always agree with the Windows API version of structure member layout.

- Specific tweaks like window subclassing, using GDI, memory mapped files, etc.  The sky's the limit.  I do this stuff all the time.


- System-level stuff like creating Windows services or implementing your own threads.  This stuff is possible, but simply too flaky for production because it violates the assumptions that VB was built on.

If you have a solid understanding of how pointers are implemented in C and used in  the Windows API, and you understand exactly how VB implements string, object, structure and primitive variables, there is very little you can't do in Windows using VB.  It normally isn't a good idea, because you are doing way more work than you would in C to accomplish the same result, and you are breaking some of the fundamental assumptions built into VB.

After many years of doing things in VB to prove that it could be done, my advice is, if it's easier to do in C, do it in C.  If you have a substantial body of code in VB and you need to just push the envelope a little, then go for it, but be very careful because you can't depend on VB to catch your errors for you when you start messing with this stuff.

Craig
Monday, March 29, 2004

Why are you trying to accomplish other than convince some naysayer that you can really use VB as a serious language?

pdq
Monday, March 29, 2004

If you're trying to do really hardcore scary-dangerous things with VB6, this book is great:

http://www.amazon.com/exec/obidos/tg/detail/-/0201707128/ref=pd_sim_books_3/102-5358762-3104963?v=glance&s=books

OTOH, if you're trying to do really hardcore scary-dangerous things, VB6 really isn't the best language.  <g>  It's fun to hack VB6, but not a great programming practice.

Robert Jacobson
Monday, March 29, 2004

Agreed with all of the above, hence "theoretically". Is there anything that can be done natively in C that *cannot* be done in VB?

KayJay
Monday, March 29, 2004

Savage:

<quote>
Callback.

Well you have late binding in VB6. So pass the function name as a string! then use

obj.strMethodName

Savage
</quote>

That doesn't work. Did you try it?

I think you meant to use CallByName. Which will achieve what you want the above code to achieve.

Seeya

 
Monday, March 29, 2004

Craig,

<quote>
- System-level stuff like creating Windows services or implementing your own threads.  This stuff is possible, but simply too flaky for production because it violates the assumptions that VB was built on.
</quote>

Actually, you can do them in VB 6 without pointers.

Seeya

 
Monday, March 29, 2004

Robert Jacobson,

<quote>
If you're trying to do really hardcore scary-dangerous things with VB6, this book is great:

http://www.amazon.com/exec/obidos/tg/detail/-/0201707128/ref=pd_sim_books_3/102-5358762-3104963?v=glance&s=books

OTOH, if you're trying to do really hardcore scary-dangerous things, VB6 really isn't the best language.  <g>  It's fun to hack VB6, but not a great programming practice.
</quote>

Hardcore Visual Basic is also excellent. You can't buy it anymore, but it is available on old MSDN Library CDs or online for free at http://www.mvps.org/vb/index2.html?hcvb.htm .

Seeya

 
Monday, March 29, 2004

KayJay

<quote>
Is there anything that can be done natively in C that *cannot* be done in VB?
</quote>

Sure. VB 6 can only do STA threading - I am assuming C supports the other models. And (as mentioned towards the top of this thread) VB 6 won't allow you to point a function pointer at another VB function (see bottom of http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconpassingfunctionpointerstodllprocedurestypelibraries.asp). Plus VB 6 can't staticly compile all its dependencies into a single EXE.

That is three I can think of off the top of my head.

BTW, I am a VB 6 programmer, so don't take offense at the above statements.

Seeya

 
Monday, March 29, 2004

Nothing to be offended by! Just an academic curiosity. :)

Thanks for the link.

KayJay
Monday, March 29, 2004


"." (The guy who signs his posts 'Seeya')

Yes you can do services in VB6 using NTSVC.OCX or lots of other very limited solutions that I have plenty of experience with.  And yes you can do threads by hacking together a bunch of ActiveX EXEs, or creating an Apartment threaded DLL and hosting it in a multithreaded process written in some other language.

Or you can do any of those things by buying third-party components that basically do all the heavy lifting in C and implement an ActiveX interface that your VB program can hook into.

My point was (specifically in answer to the OP) that you can but shouldn't do this stuff NATIVELY in VB using APIs like CreateThread().  In other words, don't try to implement services and multithreaded processes in VB the same way you would do it in C. 

My answer to the OP is still the same:  You can manage arbitrary blocks of memory using pointers in VB by cobbling together calls to CopyMemory, StrPtr, VarPtr and ObjPtr. Therefore, theoretically there is nothing you can do in C that you can't do in VB.  In practice, you're limited to the implementation of the VB runtime, so there are some classes of applications that should not be created in VB, because they will actually take more effort than their C counterparts.

Craig
Tuesday, March 30, 2004

Craig,

See your point. Well stated.

Not sure I would agree with the statement:

<quote>
Therefore, theoretically there is nothing you can do in C that you can't do in VB.
</quote>

but thats a minor quibble.

Seeya

 
Tuesday, March 30, 2004

*  Recent Topics

*  Fog Creek Home