De-Referencing A Pointer

James Dunne james.jdunne at gmail.com
Tue Mar 21 10:42:15 PST 2006


Rory Starkweather wrote:
>>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 think we've discovered in another reply thread that the source of the 
error is the use of char* instead of wchar* and the missing StrPtr() 
call on the VB side.  Learn something new every day!

> 
> 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);
> }
> 

Well, the error here (I would assume) is your use of %s instead of %.*s, 
yes arcane printf knowledge to the rescue again.

> 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);
> 

Wondering why this didn't work...

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

It's the best kinda knowledge out there!  Try looking up Sound Blaster 
16 and VGA interrupts and output ports - that's some fun arcane knowledge.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne



More information about the Digitalmars-d-learn mailing list