• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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
  • 3
  • 3
1 Solution
Posting it here:

..C file:

PURPOSE:      Calculates track lenght for MP3s
      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);
      memmove(&var, buf, 4);
      if (var==0xA0D2020) return -1;
      for(i=0; i<512; i++)
            memmove(&var, buf+i, 4);
            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*/
            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;
            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:

      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

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.
return 1;
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.
fmuftiAuthor Commented:
I have also define the functions as u have defined in DEF file.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now