Thread overview | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 21, 2006 De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Still working on a VB to D DLL interface. I want to pass a string to a D function. The only way I have been able to do it without errors is ByRef on the VB end, as expected, to a char* on the D end. However, I want to use the 'find' function on the string. 'find' requires a char[] and a dchar as inputs. I haven't been able to figure out how to de-reference the pointer for the char[] and I don't know how to produce a dchar. Here's the code: //--------DInStr.d----------- import DLLMain; import std.string; // For find extern (Windows) { int findChar(char* searchString) //int findChar(char[] searchString, dchar searchChar) { int iPointer = 0; char[] eChar = "e"; char[] anotherString; anotherString = *searchString; //<- Here's the problem iPointer = find(anotherString, eChar); // iPointer = find(*searchString, eChar); // iPointer = find(&searchString, eChar); return (iPointer); } } The error message is "Cannot explicitly convert expression (*(searchString)) of type char* to char[]. If I take out the '*' I get about the same thing. The other problem is the dchar. What can I pass to the function that it will think is a dchar? Is there any documentation on this kind of thing? I haven't had much luck interpreting the language specification. |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory Starkweather | Rory Starkweather skrev: > Still working on a VB to D DLL interface. I want to pass a string to a D > function. The only way I have been able to do it without errors is ByRef on the > VB end, as expected, to a char* on the D end. However, I want to use the 'find' > function on the string. 'find' requires a char[] and a dchar as inputs. I > I haven't been able to figure out how to de-reference the pointer for the char[] > and I don't know how to produce a dchar. a char* cStr is converted to char[] by cStr[0..std.c.string.strlen(cStr)] or std.string.toString(cStr) /Oskar |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oskar Linde | On Tue, 21 Mar 2006 09:00:15 +0100, Oskar Linde <oskar.lindeREM@OVEgmail.com> wrote:
> Rory Starkweather skrev:
>> Still working on a VB to D DLL interface. I want to pass a string to a D
>> function. The only way I have been able to do it without errors is ByRef on the
>> VB end, as expected, to a char* on the D end. However, I want to use the 'find'
>> function on the string. 'find' requires a char[] and a dchar as inputs. I
>> I haven't been able to figure out how to de-reference the pointer for the char[]
>> and I don't know how to produce a dchar.
>
> a char* cStr is converted to char[] by
>
> cStr[0..std.c.string.strlen(cStr)]
>
> or
>
> std.string.toString(cStr)
and you can get a dchar with std.utf.toUTF32().
Regan
|
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Tue, 21 Mar 2006 20:30:46 +1200, Regan Heath <regan@netwin.co.nz> wrote:
> On Tue, 21 Mar 2006 09:00:15 +0100, Oskar Linde <oskar.lindeREM@OVEgmail.com> wrote:
>> Rory Starkweather skrev:
>>> Still working on a VB to D DLL interface. I want to pass a string to a D
>>> function. The only way I have been able to do it without errors is ByRef on the
>>> VB end, as expected, to a char* on the D end. However, I want to use the 'find'
>>> function on the string. 'find' requires a char[] and a dchar as inputs. I
>>> I haven't been able to figure out how to de-reference the pointer for the char[]
>>> and I don't know how to produce a dchar.
>>
>> a char* cStr is converted to char[] by
>>
>> cStr[0..std.c.string.strlen(cStr)]
>>
>> or
>>
>> std.string.toString(cStr)
>
> and you can get a dchar with std.utf.toUTF32().
Oops, I should read the whole post before replying. Any character literal will be accepted as a dchar, eg.
void foo(dchar c) {}
void main()
{
foo('a');
}
so you'll probably have no trouble there. :)
Regan
|
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath |
>
>void foo(dchar c) {}
>void main()
>{
> foo('a');
>}
>
>so you'll probably have no trouble there. :)
>
Thanks for the help. I'm still not sure how to get VB to send a dchar, but there are only a couple of possibilities. Unless you are suggesting that I pass it to D as a string and then use a conversion function on it.
|
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory Starkweather | Rory Starkweather wrote: >>void foo(dchar c) {} >>void main() >>{ >> foo('a'); >>} >> >>so you'll probably have no trouble there. :) >> > > > > Thanks for the help. I'm still not sure how to get VB to send a dchar, but there > are only a couple of possibilities. Unless you are suggesting that I pass it to > D as a string and then use a conversion function on it. > > 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. -- -----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 |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | VB6 internally stores strings in UNICODE. When calling external functions VB6 uses stub to convert string to/from ANSI format. If you want to pass string from VB in Unicode use declaration like this: Declare Function findChar Lib "..." (ByVal t As LONG) As Long and call that function as: retValue = findChar(StrPtr("Some string")) this will pass pointer to UNICODE string to externall function findChar In article <dvp71g$hv1$1@digitaldaemon.com>, James Dunne says... > >Rory Starkweather wrote: >>>void foo(dchar c) {} >>>void main() >>>{ >>> foo('a'); >>>} >>> >>>so you'll probably have no trouble there. :) >>> >> >> >> >> Thanks for the help. I'm still not sure how to get VB to send a dchar, but there are only a couple of possibilities. Unless you are suggesting that I pass it to D as a string and then use a conversion function on it. >> >> > >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. > >-- >-----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 |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to NoBoDy | NoBoDy wrote: > VB6 internally stores strings in UNICODE. > When calling external functions VB6 uses stub to convert string to/from ANSI > format. > > If you want to pass string from VB in Unicode use declaration like this: > Declare Function findChar Lib "..." (ByVal t As LONG) As Long > > and call that function as: > retValue = findChar(StrPtr("Some string")) > > this will pass pointer to UNICODE string to externall function findChar > > > In article <dvp71g$hv1$1@digitaldaemon.com>, James Dunne says... > >>Rory Starkweather wrote: >> >>>>void foo(dchar c) {} >>>>void main() >>>>{ >>>> foo('a'); >>>>} >>>> >>>>so you'll probably have no trouble there. :) >>>> >>> >>> >>> >>>Thanks for the help. I'm still not sure how to get VB to send a dchar, but there >>>are only a couple of possibilities. Unless you are suggesting that I pass it to >>>D as a string and then use a conversion function on it. >>> >>> >> >>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. >> >>-- >>-----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 > > > Thanks! That helps a lot for me as well. So then am I still correct in that D should work with a wchar* ? BTW, do you have an MSDN reference for that info? -- -----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 |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | "James Dunne" <james.jdunne@gmail.com> wrote in message news:dvpa6h$ltk$1@digitaldaemon.com... > NoBoDy wrote: >> VB6 internally stores strings in UNICODE. >> When calling external functions VB6 uses stub to convert string to/from >> ANSI >> format. >> >> If you want to pass string from VB in Unicode use declaration like this: Declare Function findChar Lib "..." (ByVal t As LONG) As Long >> >> and call that function as: >> retValue = findChar(StrPtr("Some string")) >> >> this will pass pointer to UNICODE string to externall function findChar >> >> >> In article <dvp71g$hv1$1@digitaldaemon.com>, James Dunne says... >> >>>Rory Starkweather wrote: >>> >>>>>void foo(dchar c) {} >>>>>void main() >>>>>{ >>>>> foo('a'); >>>>>} >>>>> >>>>>so you'll probably have no trouble there. :) >>>>> >>>> >>>> >>>> >>>>Thanks for the help. I'm still not sure how to get VB to send a dchar, >>>>but there >>>>are only a couple of possibilities. Unless you are suggesting that I >>>>pass it to >>>>D as a string and then use a conversion function on it. >>>> >>>> >>> >>>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. >>> >>>-- >>>-----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 >> >> >> > > Thanks! That helps a lot for me as well. So then am I still correct in that D should work with a wchar* ? > > BTW, do you have an MSDN reference for that info? > > -- > -----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 VB strings are equivalent to COM's BSTR (which is wchar*), so you should be using SysAllocString and SysFreeString on the D side, just as you would with COM in C/C++. This article has some good pointers for interfacing VB with C++, which might help. http://www.flipcode.com/articles/article_vbdlls.shtml |
March 21, 2006 Re: De-Referencing A Pointer | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dunne | > >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. |
Copyright © 1999-2021 by the D Language Foundation