djeidot
djeidot

Reputation: 4642

How to get the executable name when accessing the program's resources through HTML?

I have a HtmlView inside my MFC program where I display some data in HTML form. The HTML makes use of some resources included within the executable file, so, in general, my HTML files include some things like:

<script language="javascript" type="text/javascript" src="res://MyProgram.exe/JS/IDR_JQUERY"></script>

or

<img src="res://MyProgram.exe/JPG/PROGRAMLOGO"/>

Problem is, if the user changes the program name from MyProgram.exe to something else, the HTML no longer works properly.

I am using CHtmlView::LoadFromResource to load the HTML file. I was already able to change the HTML at run time for the body section using this function:

BOOL DHtmlView::PutBodyContent(LPSTR lpstrContent)
{
 //check if HtmlDocument initialized
 if( m_pHtmlDoc2)
 {
  HRESULT hr = S_OK;
  IHTMLElement *pBodyElement;
  //get body element
  hr=m_pHtmlDoc2->get_body( &pBodyElement);
  //put content to body element
  _bstr_t pbBody( lpstrContent);
  hr=pBodyElement->put_innerHTML( pbBody);

  if( hr==S_FALSE) return FALSE;
  else return TRUE;
 }
 else return FALSE;
}

but I can't seem to find a way to do the same with the head section.

Do you have any hints?

Upvotes: 1

Views: 414

Answers (2)

djeidot
djeidot

Reputation: 4642

After some time meddling with IHTMLElement, I found a much easier way to do it. Since my HTML file is itself inside the program resources, I found I could just do relative references. So my examples turned to:

<script language="javascript" type="text/javascript" src="../JS/IDR_JQUERY"></script>

<img src="../JPG/PROGRAMLOGO"/>

Upvotes: 0

Moo-Juice
Moo-Juice

Reputation: 38820

Obtain the name of the executable using the API call ::GetModuleFileName() and strip away the path.

Reference the name of the executable in your HTML using some kind of an escape-sequence like "$(FileName)":

<img src="res://$(FileName)/JPG/PROGRAMLOGO"/>

Prior to setting the contents of the HTML document, replace all occurences of your escape sequence with the result of your GetModuleFileName() API call.

Upvotes: 1

Related Questions