Calling Dll in VB...

I wanted to call C dll in VB.
Can someone help me. Just mail me a CPP and DEF file for C Dll, and  source
code for VB having one function with one argument that is passed by VB to Function in C dll. I hope by working on that I'll able to do my required job( Making and calling complex functions).Email:fmufti@softhome.net
fmuftiAsked:
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.

MikeP090797Commented:
Posting it here:


..C file:

/*
PURPOSE:      Calculates track lenght for MP3s
ARGUMENTS:
      IN szFile:            Track to check
RETURNS:      The lenght of the track in seconds, or -1 if failed
NOTES:            Supports layer 1 MPEG3 files ONLY
*/

long _stdcall
GetTrackLength(char *szFile)
{
      int var=0;
      int hFile;
      int i;
      long lng;
      int bitrate;
      int freq;
      int freqN;
      char buf[512];
      int fl;
      int layer;
      BOOL flag;

      float arr[3][3] = {            {8.707483f,  8.0f, 12.0f},
                                          {26.12245f, 24.0f, 36.0f},
                                 {26.12245f, 24.0f, 36.0f}
                                      };
      
      hFile=_open(szFile, _O_BINARY | _O_RDONLY);
      _read(hFile, &buf, sizeof(buf));
      fl=_lseek(hFile,0, SEEK_END);
      _close(hFile);
      
      memmove(&var, buf, 4);
      if (var==0xA0D2020) return -1;
      
      for(i=0; i<512; i++)
      {
            memmove(&var, buf+i, 4);
            lng=swap_int32(var);
            if (((lng >> 21) & 0x7FF) != 0x7FF) continue;
            if (((lng >> 20) & 0x1) == 0) continue;        /*Only version 1 and 2 are valid*/
            if (((lng >> 17) & 0x3) == 0) continue;        /*00 is invalid layer*/
            if (((lng >> 12) & 0xF) == 0xF) continue; /*1111 is invalid bitrate*/
            if (((lng >> 10) & 0x3) == 0x3) continue; /*11 is invalid fequency*/
            if ((lng & 0x3) == 0x2) continue;              /*10 is invalid*/
            flag=TRUE;
            break;
      }
      
      if(flag)
      {
            bitrate=(lng >> 12) & 0xF;
            freq=(lng >> 10) & 0x3;
            
            if(bitrate>0x0 && bitrate<0x6) bitrate = bitrate * 8000 + 24000;
            if(bitrate>0x5 && bitrate<0xA) bitrate = 80000 + (bitrate - 6) * 16000;
            if(bitrate>0x9 && bitrate<0xF) bitrate = 160000 + (bitrate-10) * 32000;
            
            freqN=freq;
            layer=(lng >> 17) & 0x3;
            
            if(layer!=1) return -1;
            if(freq==0) freq = 44100;
            if(freq==1) freq = 48000;
            if(freq==2) freq = 32000;
            return (LONG)((fl / (144 * bitrate / freq + ((lng>>0x09) & 0x1)))*arr[3-layer][freqN]/1000);
      }

      return -1;
}


..DEF file:

LIBRARY MP3APL
VERSION 5.12
EXPORTS
      GetTrackLength      @1


Call from VB:

'put this line in a module:

Declare Function GetTrackLength Lib "MP3APL.DLL" (ByVal szFile As String) As Long


'To call:

Dim L as long
L=GetTrackLength("C:\12.mp3")


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
fmuftiAuthor Commented:
Thanx u have give a pritty lenghty routine. I want a simple routine. So that I can understand it better. I write the fol code

int MyBeep(void)
{
// Beep
//MessageBeep( (WORD) -1 );
return 1;
}
int MyDelay( long wait )
{
// Delay.
Sleep(wait);
return 1;
}
in
VB I declare in module as:
Declare Function MyDelay Lib "C:\vb5prg\dll\mydll\Release\MyDll" (ByVal wait As Long) As Integer
Declare Function MyBeep Lib "C:\vb5prg\dll\mydll\Release\MyDll" () As Integer
I then made a button in a form with fol code:
Private Sub Command1_Click()
Dim l As Long
Dim p As Long
p = MyBeep
l = MyDelay(500)
End Sub
My Beep Message works fine as it does not take any arguments, but for MyDelay it gives error"Error 49, Bad Dll calling conversion. Can u guide me why. ? In fact I have to write a simple routine in VB which uses dll to send data on Parallel  port. That data is to be send from vb to dll.
I hope now u can be able to reply me more precisely.
0
fmuftiAuthor Commented:
I have also define the functions as u have defined in DEF file.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

MikeP090797Commented:
The problem is with the declaration: In C++, int is 32 bit, so actualy there is no difference between it and long. In VB, Integer is 16 bit, so this is why the error occures. It should be:



Declare Function MyDelay Lib "C:\vb5prg\dll\mydll\Release\MyDll" (ByVal wait As Long) As Long
Declare Function MyBeep Lib "C:\vb5prg\dll\mydll\Release\MyDll" () As Long

0
fmuftiAuthor Commented:
I have tried but again failure, my email is fmuti@softhome.net, can u mail the code that I have written after checking it on VC and VB. So that I can mail u if required in return the code and u'll be able to find the problem.
0
MikeP090797Commented:
Just sent
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.