De-Referencing A Pointer

Rory Starkweather Rory_member at pathlink.com
Tue Mar 21 10:04:50 PST 2006


>
>Actually you want to use wchar* at the D end from VB.  VB sends 
>'Unicode' strings where each character is represented by two bytes. 
>This is the equivalent of a wchar* in D.  char, wchar, and dchar in D 
>are all more-or-less interchangeable.  The best way to get VB and D to 
>do string work together is this:
>
>Declare Function findChar Lib "..." (ByVal t As String) As Long
>
>export extern (Windows) int findChar(wchar* str) {
>	...

>I'm not 100% sure if this works as I've had serious issues with VB6's 
>string handling in the past (especially in scenarios like this).  I 
>think the trick is to initialize the string from VB's side and pass it 
>in to C/C++/D for them to modify it.  I'll do some more research on this 
>and get you a better answer if I can.
>

I may be running into that now. I decided to forget about the dchar issue and
use another version of find: find (char[] s, char[] sub)

I don't get any compile errors or nasty comments from VB, but the value I get
back indicates that the character is not in the strin (-1)

************************** Here's the VB stuff

Public Declare Function findACharInString _
Lib "E:\DigitalMars\Work\DInStr.dll" _
(ByRef strString As String, _
ByRef strSearchChar As String) _
As Long

Private Sub cmdTest1_Click()

Dim lngCharPointer As Long
Dim strString As String
Dim strChar As String * 1

strString = "Fred"
strChar = "e"

lngCharPointer = findACharInString(strString, strChar)

MsgBox strChar & " found at " & CStr(lngCharPointer)

End Sub

******************************* Here's the D code:

//--------DInStr.d-----------
import DLLMain;
import std.string;      // For find

extern (Windows)
{
int findACharInString(char* searchString, char* searchChar)
{
int iPointer = 0;
char[] theChar;
char[] theString;

theString = std.string.toString(searchString);
theChar = std.string.toString(searchChar);

iPointer = find(theString, theChar);

return (iPointer);
}
}

I figured that the string and the character might not look the same after they
were massaged, so I added this:

void main(char[][] Args)
{
int iRetVal;

iRetVal = findACharInString(Args[1], Args[2]);

printf("%s found at %d\n", Args[2], iRetVal);
}

That just causes more problems. When I try to run it I get an Access Violation.
(?) I can't find that note on the caveat for printf and strings, so I assume I'm
running into that problem here. I originally tried using writefln in
findACharInString and consistently got "Unknown software exception (0xe0440001)
occurred in app at location 0x7c59bc3f. (I was surprised that it was so
consistent.) So I must not understand that function either.

I think the way I tried to do it looked like this:

writefln("The string: %s", theString);
writefln("The character: %s", theChar);

Its amazing that so many things can go wrong with a program that is only 7 lines
long. Part of the problem is that it has been 10 years since I have used C, and
I have never used D before this week. There seems to be a hump in learning "new"
programming languages. One day nothing works the first ten times and the next
day about half of what you write works the first time. I'm not at that point
yet.

Rory


Aggressively seeking arcane knowledge.



More information about the Digitalmars-d-learn mailing list