Page 1 of 1

MSVC++ lib strange behavior

Posted: Wed Aug 09, 2006 10:24 pm
by r_hyde
I'm using a lib compiled with MSVC++6 using Import..EndImport. I have tested all the functions in the lib, and they all work fine as long as they are not used within procedures. Once I put a call to a lib function inside a procedure I get some pretty odd behavior. Example:

(I'm sorry that I cannot provide a more useful code example because I'm not at liberty to distribute the .lib in question or to reveal my full source.)

Code: Select all

Import "mylib.lib"
  MyFunction1(param$)
EndImport

Procedure test1(path$)
  dir = ExamineDirectory(#PB_Any, path$, "")
  If IsDirectory(dir)
    While NextDirectoryEntry(dir)
      MyFunction1(DirectoryEntryName(dir))
    Wend
    FinishDirectory(dir)
  EndIf
EndProcedure

mypath$ = "D:\test"
test1(mypath$)
This extremely minimal example (generalized and simplified from my actual code) fails on NextDirectoryEntry(dir), saying that the directory is not initialized even though the call to IsDirectory(dir) passes. If I comment out the call to the lib function it works perfectly. I find this very strange. In other cases I have run into invalid memory access errors calling the lib functions ONLY when they are called within procedures. Is this a known problem or bug? Am I doing something wrong? I'm pulling my hair out over this!

Posted: Wed Aug 09, 2006 10:44 pm
by jack
I suspect the lib uses cdecl calling convention, try using ImportC instead.

Posted: Thu Aug 10, 2006 4:52 am
by r_hyde
Spot on, jack! Working beautifully now.