I wrote a simple DLL for Dark Basic Pro and it returns a string without a problem in XP. In Vista, unless I put the compatibility mode in Win 98/Me, it crashes. Does anyone have any ideas as to why it works flawlessly in one and not the other? (Code below)
In the InitialiseCorePtr() function you retrieve a structure pointer from a dll which, if I am correct, contains function pointers amongst other things. You then close the library. Well, if these function pointers are pointing to functions inside what is a now closed library.... CRASH.
Just wondering? I've probably misread the code!
I may look like a mule, but I'm not a complete ass.
I'm not sure I understand fully what it does. This is the header that is given in their tutorial on how to do this.
It works in XP flawlessly. In fact, before I stripped it down to what you see here, it was retrieving data from a file, massaging it, and giving back an answer in the form of a string with over ten thousand records. It's only when I use this in Vista that it causes the problem. Someone else tried it with Windows 7 and got it as well.
Lastly, I can return integers, floats, and doubles with Vista with no problem. It just seems to be strings which is what is baffling to me.
Randy
Maybe it's just a bunch of stuff that happens -- Homer Simpson
I commented out the closelibrary() function and it didn't make a difference for XP which is good. It did affect Vista in that it partially worked. Now it only crashes the majority of the time instead of all the time.
Maybe it's just a bunch of stuff that happens -- Homer Simpson
I would search the code looking for similar problems because I have never had problems with Vista and dll's etc.
You have to always take care when returning strings from dll's in that you don't want either the dll or the client attempting to free string memory which has been allocated in the other. This is a common problem and invariably leads to crashes. The best way of returning strings is via a buffer or a pointer to a global string etc. so have a good look at the code of both the dll and the client.
I may look like a mule, but I'm not a complete ass.
Apparently PB 4.xx has made some improvements. I just found that I no longer need to worry with addresses and pointers. All I had to do was define the procedure as procedure.s and simply return gret$. No @ or * or anything of the sort.
It was one of those moments where I said, "I've tried everything I can think of so let's get desperate!"
I'll have to post how to do this for other PB'ers in case anyone wants to know. It works for XP and Vista and hopefully Version 7.
Randy
Maybe it's just a bunch of stuff that happens -- Homer Simpson
That has always been an option with Purebasic as I understand it.
Personally I tend to avoid using global strings that way because they are not necessarily threadsafe (unless the string is constant). I use buffers instead.
I may look like a mule, but I'm not a complete ass.
If you have a dll function from which you wish to return a string then instead have it return the length (in characters) of the string. Have it also take a parameter which points to the address of a buffer which the client is responsible for allocating. If this parameter is non-zero then poke the string into the buffer.
What the client will typically do is call the function to get the required length of the string. It then uses something like buffer$ = Space(numCharacters) and then calls the function again with @buffer$ as the aforementioned parameter.
On return, buffer$ will contain the string.
You can get away with calling the function just once if you know the maximum length of the string to be returned etc.
I may look like a mule, but I'm not a complete ass.
The DBP global structure stores a 32-bit address to an internal function CreateDeleteString. You can use this to return a string to DBP that it can then free later without risking the string being freed twice.
I tried that too. Vista and apparently Windows 7 somehow 'leak'. I can get it to return a string about eight times before it crashes now instead of immediately. However, I'm going to play around with it again and see.
I'll report any findings in a little while.
Thanks Srod and Mistral for your knowledge and help,
Randy
Maybe it's just a bunch of stuff that happens -- Homer Simpson
Did anyone actually get to the bottom of this - I am trying to create functions returning strings with ProcedureCDLL.l for commands in to DarkBasicPro (latest versions of DBPro and PB owned) and I get crashes (in Windows 7 and Vista) -
Has anyone figured out the secret to it that seems to have illuded everyone in this thread???