Link to home
Start Free TrialLog in
Avatar of rayford
rayford

asked on

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);      
          }
      
Avatar of AnswerTheMan
AnswerTheMan

<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.

 
>><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.
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.
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?
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?
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...
Avatar of rayford

ASKER

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!!
Avatar of rayford

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of AnswerTheMan
AnswerTheMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i've forgot to mention :
copy the Tempo.DLL file to your win\sys dir before runnin the VB coed.
one more :
in vc++, when selecting a new project, there are some DLL options.
select the "Win 32 Dynamic Link library"
option.
Avatar of rayford

ASKER

THANKS
Avatar of rayford

ASKER

thanks