Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB and C++ dll

VB and C++ dll

Scheduled Pinned Locked Moved Visual Basic
questionc++data-structuresdebugginghelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tomaz Rotovnik
    wrote on last edited by
    #1

    Hi I created very simple dll (vc++) which has three functions (start, stop and initialization). it starts capturing sound from soundblaster and when the buffer is filled with the data, dll calls VB calback function (passed with initialization) and passes the buffer size (integer type). First I have some problems with __stdcall declaration in VC dll, but I think I solved that problem. Callback function runs in separated thread under main dll thread. Declaration of initialization of callback function looks like this: __declspec(dllexport) int __stdcall RecToR_Init_Cap(void (__stdcall *tmp)(int_4 size)); IN VB: Public Delegate Sub Callback(ByVal size As Int32) Public Declare Function RecToR_Init_Cap Lib "RecToR_Cap_TR.dll" (ByVal CalBckFunc As Callback) As Int32 "Private Sub Button1_Click" event in VB: Dim cb As Callback cb = AddressOf CallBackFunc Success = RecToR_Init_Cap(cb) Public Sub CallBackFunc(ByVal size As Int32) TextBox1.Text = size & vbCrLf End Sub When I run the application, the callback function is called 50 times and then the application crashes: "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Unknown Module. Additional information: External component has thrown an exception. An exception 'System.NullReferenceException' has occured in..." I think I override stack pointer. How can I trace down the problem? From Microsoft articles I get information that I cannot call callbacks from spawned threads within my DLL. Is it possible to overcome this problem? Thanks for any idea Tomaz Rotovnik

    D 1 Reply Last reply
    0
    • T Tomaz Rotovnik

      Hi I created very simple dll (vc++) which has three functions (start, stop and initialization). it starts capturing sound from soundblaster and when the buffer is filled with the data, dll calls VB calback function (passed with initialization) and passes the buffer size (integer type). First I have some problems with __stdcall declaration in VC dll, but I think I solved that problem. Callback function runs in separated thread under main dll thread. Declaration of initialization of callback function looks like this: __declspec(dllexport) int __stdcall RecToR_Init_Cap(void (__stdcall *tmp)(int_4 size)); IN VB: Public Delegate Sub Callback(ByVal size As Int32) Public Declare Function RecToR_Init_Cap Lib "RecToR_Cap_TR.dll" (ByVal CalBckFunc As Callback) As Int32 "Private Sub Button1_Click" event in VB: Dim cb As Callback cb = AddressOf CallBackFunc Success = RecToR_Init_Cap(cb) Public Sub CallBackFunc(ByVal size As Int32) TextBox1.Text = size & vbCrLf End Sub When I run the application, the callback function is called 50 times and then the application crashes: "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Unknown Module. Additional information: External component has thrown an exception. An exception 'System.NullReferenceException' has occured in..." I think I override stack pointer. How can I trace down the problem? From Microsoft articles I get information that I cannot call callbacks from spawned threads within my DLL. Is it possible to overcome this problem? Thanks for any idea Tomaz Rotovnik

      D Offline
      D Offline
      DK KiloDunse
      wrote on last edited by
      #2

      Could be the garbage collector. Try to put this code when you want to stop recording. GC.KeepAlive(cb) I had the same problem with callback function when capturing video.

      T 1 Reply Last reply
      0
      • D DK KiloDunse

        Could be the garbage collector. Try to put this code when you want to stop recording. GC.KeepAlive(cb) I had the same problem with callback function when capturing video.

        T Offline
        T Offline
        Tomaz Rotovnik
        wrote on last edited by
        #3

        Hi When the callback function is trigered 50 times I still capturing the sound. I didn't stop sound capturing. Do I need to call GC.KeepAlive(cb) inside the callback function? Tomaz Rotovnik

        D 1 Reply Last reply
        0
        • T Tomaz Rotovnik

          Hi When the callback function is trigered 50 times I still capturing the sound. I didn't stop sound capturing. Do I need to call GC.KeepAlive(cb) inside the callback function? Tomaz Rotovnik

          D Offline
          D Offline
          DK KiloDunse
          wrote on last edited by
          #4

          The statement GC.KeepAlive is a way to keep a reference to an object. When the garbage collector "scans" your code, and there isn’t a further reference to an object, it removes it. E.g. the reference to your callback function is lost. In your code…. When the recording is finished, you should call GC.KeepAlive(cb) That way you will not loose your reference.

          T 1 Reply Last reply
          0
          • D DK KiloDunse

            The statement GC.KeepAlive is a way to keep a reference to an object. When the garbage collector "scans" your code, and there isn’t a further reference to an object, it removes it. E.g. the reference to your callback function is lost. In your code…. When the recording is finished, you should call GC.KeepAlive(cb) That way you will not loose your reference.

            T Offline
            T Offline
            Tomaz Rotovnik
            wrote on last edited by
            #5

            OK Now I understand. Unfortunately dll is still capturing when the error arrise (I didn't call stop function at all) Tomaz Rotovnik

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups