The simplest way to use C code from VB

For instance say I want to do a faster directory tree search like this code offers instead of using API calls or adding a Folder or File control to the forms and reading from it take this C code and show the easy way to call this from VB and return the filenames\paths for credit:
            
    //***************************************************************
    // Name: How to search a directory tree For files
    // Description:The following code sample illustrates how to search from a given directory downward through the entire directory tree. The sample output from this example is directed to the system debug screen.
    For this example, the first class member (below) is called by a menu item. After the search is finished, a message box pops up. http://www.concentric.net/~cgalbrai/dirsrh.shtml
    // By: Found On the World Wide Web
    //
    //
    // Inputs:None
    //
    // Returns:None
    //
    //Assumes:None
    //
    //Side Effects:None
    //
    //Code provided by Planet Source Code(tm) (http://www.Planet-Source-Code.com) 'as is', without warranties as to performance, fitness, merchantability,and any other warranty (whether expressed or implied).
    //***************************************************************
   
    void CTestView::OnSearch()
          {
          
          // szFilename is declared in the header as array of char
          // look For MyFile.txt (or whatever)
          
                strcpy(szFilename,"MyFile.txt");
          
          // go to root directory (or to whichever directory that you wish)
          
                _chdir("C:\\");
          
          // search For the filename
          
                SearchDirectory();
          
          // announce when done
          
                MessageBox("Done Searching");      
          }
    SearchDirectory() is called initially from OnSearch(). SearchDirectory() is Then called recursively (from itself) until the End of the directory tree is reached and all branches are searched.
          void CTestView::SearchDirectory()
          {
                struct _finddata_t filestruct;
                long hnd;
                char buffer[_MAX_PATH];
          
          // set _findfirst to find everthing
          
                hnd = _findfirst("*",&filestruct);
          
          // If handle fails, drive is empty...
          
                if((hnd == -1)) return;
          
          // Get first entity on drive - check If it's a directory
          
                if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY
                      && !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) {
                
          // If so, change to that directory and recursively call SearchDirectory
                
                      if(*filestruct.name != '.') {
                      
                            _chdir(filestruct.name);
                      
                            SearchDirectory();
          // go back up one directory level
                      
                            _chdir("..");
                      }
                }      
                else {
          // If it's not a directory and it matches what you want...
                      if(!stricmp(filestruct.name,szFilename)) {
          // output the filename With path to debugger
                            _getcwd(buffer,_MAX_PATH);
                            strcat(buffer,"\\");
                            strcat(buffer,filestruct.name);
                            strcat(buffer,"\r\n");
                            OutputDebugString(buffer);
                      }            
                }
          
                while(!(_findnext(hnd,&filestruct))) {
                
                      if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY
                            && !(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_HIDDEN)) {
                      
                            if(*filestruct.name != '.') {
                                  _chdir(filestruct.name);
                            
                                  SearchDirectory();
                            
                                  _chdir("..");
                            }
                      }
                      else {
                      
                            if(!stricmp(filestruct.name,szFilename)) {
                                  _getcwd(buffer,_MAX_PATH);
                                  strcat(buffer,"\\");
                                  strcat(buffer,filestruct.name);
                                  strcat(buffer,"\r\n");
                                  OutputDebugString(buffer);
                            }
                      }
                }
          
                _findclose(hnd);      
          }
      
LVL 1
rayfordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnswerTheManCommented:
<For instance say I want to do a
<faster directory tree search.....

C is definatly faster then VB.
however, this is TRUE ONLY when the
code does not getiing close to HardDisk
activities.
The moment your app start to deal with
the HardDisk - your app speed GOES DOWN
to the HardDisk Speed, WHICH is SLOWER THEN C AND VB....
so working in C in HardDisk speed won't give you any speed advantage.

 
0
KDivadCommented:
>><For instance say I want to do a
<faster directory tree search.....

C is definatly faster then VB.
however, this is TRUE ONLY when the
code does not getiing close to HardDisk
activities.
The moment your app start to deal with
the HardDisk - your app speed GOES DOWN
to the HardDisk Speed, WHICH is SLOWER THEN C AND VB....
so working in C in HardDisk speed won't give you any speed advantage. <<

Except for one little detail: After doing hd access, windows has a tendency to cache this info for the next access. Therefore, the next access will possibly be working out of memory instead of off the hd, in which case C++ IS faster.

After fresh boot, go to DOS and type "dir /s" and hit enter. Do it again. The second time runs from MEMORY and NOT the hd, so is a LOT faster.
0
AnswerTheManCommented:
what you saying is right for both VB or C under windows. no diffrence.
every first load of component takes more time then next time. that does not mean that the component stops dealing with HD all the time. it is.
i wrote for years C code accesing HD, and it's not faster then VB. not when doing files.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

InformativeCommented:
Still I think the question is valid where he is asking "FOR INSTANCE" take this small snip of C code and knowing can we and how to setup some C Code to be called from within VB would be definitely be useful.  Can't say I know how and apparently none of the other experts know either.  :)

I think I recall hearing one of the ways was to create a component like an OCX control and exposing some properties to VB or whatever.

Anyone else here this?
0
InformativeCommented:
I think the question was about using C code from within VB and not about a debate on which is faster?  Did anyone else find humor in the proposed answer?
0
AnswerTheManCommented:
the proposed answer was meant to be a comment, and the 'Answer' was pressed by mistake.
to the point : i can help the asker and everyone who finds interest by contributing real code and a way to do it, but it seems that that the guy who asked the Q - disapeared....no comment from him after the Q.
maybe he lost interest...
0
rayfordAuthor Commented:
Nah I just been busy sometimes can only check back every few days sorry bout that.  

Hehe I've done that myself whoops I meant comment!!
0
rayfordAuthor Commented:
So there is no way to run ANY C code from within VB for sure you guys are kidding me?  I used to be able to do it VERY easily from QuickBasic the great grandpa to VB.

I know many OCX controls are written in C perhaps simply some code to create an OCX in VC and expose a property so we can add it to VB?  

I'd increase the points but now that its been hosed with an answer recovering the points should no answer come up would be a quirky problem you know how this system works bleh!

Thanks Gang!
0
AnswerTheManCommented:
ok then.
open VC++, start new *DLL* project. call it *Tempo*.
now the Tempo.CPP file should look like this :
---------------------------------
// TEMPO.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
extern "C" __declspec(dllexport) LONG getNumber()
      {
          return 5;
      }

// the DLL file should be placed in win\sys folder/
// the VB use for the above Function will be :
// Public Declare Function getNumber Lib "TEMPO.DLL" () As Long

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
    return TRUE;
}
-------------------------------------
Note that the function that i've added and is our interest is *getNumber()*.
since it's a test project, it's just returns *5* as a Long variable.

build and compile the dll. exit vc++.

now, open a new VB project (standard exe).
add a module.

paste this line into the module code :

---------------------------------------
Public Declare Function getNumber Lib "TEMPO.DLL" () As Long
--------------------------------------

in Form1 code - paste this code :

-------------------------------------
Private Sub Form_Load()
    Dim i As Long
    i = getNumber
    MsgBox i
End Sub
--------------------------------------

run the prog. if you get the MsgBox with the number *5* in it (I do) - in means that you run C code from your VB code.

implement that to whatever functionality you need.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AnswerTheManCommented:
i've forgot to mention :
copy the Tempo.DLL file to your win\sys dir before runnin the VB coed.
0
AnswerTheManCommented:
one more :
in vc++, when selecting a new project, there are some DLL options.
select the "Win 32 Dynamic Link library"
option.
0
rayfordAuthor Commented:
THANKS
0
rayfordAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.