• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

String type to word type

 I have an activeX from Microsoft which has some functions that include a Word type variable. That variable is actually a string (like 'id=myID'). On msdn they pass that variable to the function like "id=myID", and all the info they give is that this is an OleChar variable. Thay can do that because C has almost no type checking, but Delphi has strong type checking...
  Now, I need to know how can I pass a string as a Word to this function. There must be a way to do it, maybe with pointers...
0
Bindza
Asked:
Bindza
  • 8
  • 4
  • 2
  • +3
1 Solution
 
kretzschmarCommented:
try to use PChar or PAnsichar in delphi, instead of string -> there are conversion-functions

-> just a guess -> no activeX-experience

meikl ;-)
0
 
BindzaAuthor Commented:
It's got nothing to with ActiveX in particular, I just said that so people would know what's it all about.

I forgot to add, I tried this:

var
 w: word;
 s: string;
 p: ^word;
begin
s := 'myid=SomeId';
p := @s;
w := p^;
TheMSFunction(w);
[...]

But the function returns "?" instead of "myid=SomeId".
0
 
kretzschmarCommented:
are you sure that not a DWord is used?

try this

TheMSFunction(integer(PChar('myid=SomeId'));


0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Wim ten BrinkSelf-employed developerCommented:
I just think there's a flaw in the type library you're using. Which control are we talking about here?

Or perhaps it's an OVERLOADED method...
0
 
BlackTigerXCommented:
can you show the code you have that shows how they pass the "id=myID" to the function?

also, from the Delphi IDE, when you are in the parameters part of that function and you hit Ctrl+Shift+Space Bar, what does the parameter type say?
0
 
BindzaAuthor Commented:
It's WebBrowser that is DHTMLEd, the function (IMarkupServices.CreateElement) as MS presents it is:

HRESULT CreateElement(ELEMENT_TAG_ID tagID,
    OLECHAR* pchAttributes, // here - delphi sees it as Word
    IHTMLElement** ppElement
);
Parameters

tagID
[in] Value of type ELEMENT_TAG_ID that specifies the type of tag to create.
pchAttributes
[in] Pointer to a variable of type OLECHAR that specifies the attributes of the element.
ppElement
[out] Address of a pointer to an IHTMLElement interface that returns the newly created element.

And this is how they use it:

IMarkupServices::CreateElement ( TAGID_B, "id=anID", & pElement ) will create a new b element with the attribute IHTMLElement::id set to "anID".

This is how I tried to use it:

uses MSHTML_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 w: word;
 s: string;
 p: ^word;
begin
//w := 0; for no attributes
s := 'src="C:\tmp\logomain.jpg"';
p := @s;
w := p^;
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, w, e);
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end;
0
 
BindzaAuthor Commented:
in WebBrowser that would be:

[...]
(WebBrowser1.Document as IHTMLDocument2).QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, w, e);
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end
0
 
BlackTigerXCommented:
have you tried typecasting to Word:

procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 w: word;
 s: string;
 p: ^word;
begin
//w := 0; for no attributes
s := 'src="C:\tmp\logomain.jpg"';
//p := @s; //don't need this
//w := p^; //don't need this
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, Word(PChar(s)), e); //typecast here
  showmessage(e.outerhtml);// shows "<IMG ?>" instead of <IMG src="C:\tmp\logomain.jpg">
 end;
end;
0
 
BindzaAuthor Commented:
I think I have and it said "Invalid typecast"...
0
 
Slick812Commented:
Hello  Bindza, I'm sorry I do not have time to actually test this code before I post it, but I think I would try and just Typecaste a PWChar to a PWORD


procedure TForm1.Button1Click(Sender: TObject);
var
 i: IMarkupServices;
 e: IHTMLElement;
 s: string;
 pWCha1: PWChar;
begin
s := 'src="C:\tmp\logomain.jpg"';
pWCha1 := StringToOleStr(s);
KSEditx1.DOM.QueryInterface(IID_IMarkupServices, i);
if i <> nil then
 begin
  i.createElement(TAGID_IMG, PWORD(pWCha1)^, e);
  showmessage(e.outerhtml);
 end;
end;


but I am conpleatly dumbfounded by the Delphi code heads making a PWChar to a Word for that parameter?
0
 
Lee_NoverCommented:
agreed with slick :-)
delphi should still import it as a pointer of some sort
if it doesn't work manually change the interface so that the param becomes a PWideChar or maybe 'var WideString'
0
 
BindzaAuthor Commented:
I'll try it.
0
 
BindzaAuthor Commented:
Thanks Slick, I tried it and it works perfectly :)
Now, if you could tell me how to convert a string like "TAGID_BUTTON" to a word like "TAGID_BUTTON" you'll get more points. I'm not really sure that this is a word, it may be an integer, in MSHTML_TLB it's defined like this:

// Constants for enum _ELEMENT_TAG_ID
type
  _ELEMENT_TAG_ID = TOleEnum;
const
  TAGID_NULL = $00000000;
  TAGID_UNKNOWN = $00000001;
  TAGID_A = $00000002;
  [...]
  TAGID_BUTTON = $00000011;

and the commands are defined like this (they are integers, obviously):

  IDM_UNKNOWN               =  0;
  IDM_ALIGNBOTTOM           =  1;
  IDM_ALIGNHORIZONTALCENTERS=  2;
  IDM_ALIGNLEFT             =  3;
  [...]

I need this so the user can specify which element he/she wants to insert, and also with the mshtml IDM_SOMETHING commands (there are so many of them that trying them all, by modifying the source and recompiling, would take forever, and I don't want a 200 line "case" or "if" statement).
0
 
BindzaAuthor Commented:
So, What do you think, Slick?
0
 
Lee_NoverCommented:
no conversion is needed .. those consts already are integers
in delphi $0001 is a number in HEX format (in VB it's a string variable)
0
 
Slick812Commented:
???
these are all Number (Integer) constant values, and You seem to want some sore of conversion like

function convertS(aName: String): Integer;
begin
Result := someMagicAPI_Fuction(PChar(aName));
end;

but I do not know of anything like that

SO

GUESS WHAT

it seems to me the only way to get this type of thing is with an if test or case test for ALL the "String Names" you want to have for your users to use, , and then convert that string name to the constant value for that element type

I think this is standard procedure for having user "words" for programming code types
0
 
Slick812Commented:
Oh the only alternative I can thing of is to use the string for the numeric value

EName := '$11';

function convertS(aName: String): Integer;
begin
Result := StrToInt(aName);
end;

but I do not think this would be very popular with your users
0
 
Slick812Commented:
you may could use a ComboBox with element Names like "Bold" , "Button" , "Link" or  "Image" and then set the Constant by the index of the Combo Box
0
 
BindzaAuthor Commented:
Very good idea, thanks. I wonder why didn't I think of that...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 8
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now