Please help !! ... (calling unmanaged code)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have made this simple funcion running into a eVC++ .dll - just displaying Hi World

void Test(
{ HWND hWnd
HDC hDc
hWnd=GetActiveWindow()
hDc= GetDC(hWnd)
ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4, NULL)
ReleaseDC(hWnd,hDc);

If I run this in C, outside the .dll works fine - more complex code using rotated fonts also run well

But when I call this from VB.Net using a Form - pushing a button - <DllImport("Test.dll", SetLastError:=True)> Public Shared Function Test() End Functio
then nothing happens .. never display anything ..

What's going on ? .. please help ! .. thanksss :-

PD: The point of all this is displaying rotated font in Pocket Pc, not available in .Net compact framework
 
Is the DLL exporting the function? Is the export unmangled? Do a Google on
this, as we discuss it regularly. I have a very simple example here:

http://opennetcf.org/forums/topic.asp?TOPIC_ID=255

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


AlaSoft said:
I have made this simple funcion running into a eVC++ .dll - just displaying Hi World -

void Test()
{ HWND hWnd;
HDC hDc;
hWnd=GetActiveWindow();
hDc= GetDC(hWnd);
ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4, NULL);
ReleaseDC(hWnd,hDc);}

If I run this in C, outside the .dll works fine - more complex code using rotated fonts also run well -

But when I call this from VB.Net using a Form - pushing a button -
then nothing happens .. never display anything ..

What's going on ? .. please help ! .. thanksss :-)

PD: The point of all this is displaying rotated font in Pocket Pc, not
available in .Net compact framework
 
I'm not a VB.NET developer, but looking at the .NET CF quick starts (e.g.
http://samples.gotdotnet.com/quicks...s/waitforsingleobject/waitforsingleobject.src)
the declaration of the native API is different. Something like this:

Declare Function Test Lib "Test.dll" ()

I have not tried it, but perhaps you can give it a try. Looking to the
behavior you experience right now it seems that you have a managed function
called Test that simply does nothing.

--
Regards,

Maarten Struys, eMVP
PTS Software bv

www.opennetcf.org | www.dotnetfordevices.com

AlaSoft said:
I have made this simple funcion running into a eVC++ .dll - just displaying Hi World -

void Test()
{ HWND hWnd;
HDC hDc;
hWnd=GetActiveWindow();
hDc= GetDC(hWnd);
ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4, NULL);
ReleaseDC(hWnd,hDc);}

If I run this in C, outside the .dll works fine - more complex code using rotated fonts also run well -

But when I call this from VB.Net using a Form - pushing a button -
then nothing happens .. never display anything ..

What's going on ? .. please help ! .. thanksss :-)

PD: The point of all this is displaying rotated font in Pocket Pc, not
available in .Net compact framework
 
If I were to venture a guess, you are probably calling this in some event
handler. I think the active window is not exactly what you expect it to be.
Try redefining the function to accept HWND and then use Control.Capture =
True and the PInvoke GetCapture to obtain a handle of the control you want
to draw on.

Additionally you may want to check for the return values from
GetActiveWindow and GetDC.

The fact that you are not getting an exception means that the PInvoke part
probably works ok.
 
Thanks

I think yes .. see the followin

#include "stdafx.h

BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
return TRUE


extern "C" __declspec(dllexport) int __stdcall Prueba(HWND hWnd

HDC hDc
HWND hWndDos
BOOL nSalida

MessageBox(NULL,_T("Entrada"),_T("Atención"),MB_OK)
if (hWnd=NULL
{MessageBox(NULL,_T("ERROR: Manejador de Ventana Nulo"),_T("Atención"),MB_OK);
hDc=GetWindowDC(hWnd)
if (hDc=NULL
{MessageBox(hWnd,_T("ERROR: Contexto Nulo"),_T("Atención"),MB_OK);
nSalida=ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4, NULL)
if (nSalida=0
{MessageBox(hWnd,_T("ERROR: No se desplego el literal"),_T("Atención"),MB_OK);
SetWindowText(hWnd,_T("PROCESO EN MARCHA"))
ReleaseDC(hWnd,hDc)
hWndDos=FindWindow(NULL,_T("Prueba"))
return (int)hWndDos



Now the point here is .. that this code seems to work .. but again, it doesn't display anything. See that SetWindowText(...) ? ..
well inside the .dll, doesn't change the caption ... outside - invoked in VB.Net as a lonely call - yes ! ..

Note: The MessageBox(..) inside the .dll .. works fine ! .. - it shows just the first, because there are no errors (hDC=Null, etc)

In fact, this is the code written in VB.Net

Dim nHandle As IntPt
Dim nHandle2 As Intege
Dim nContexto As IntPt
Dim nNumero As IntPt

Me.Capture = Tru
// imported GetCaptur
nHandle = GetCapture(
Me.Capture = Fals

// Prueba() here is my Test function inside de .dll .
nHandle2 = Prueba(nHandle

// nHandle=nHandle2 !!!! ... so is working ..

// imported SetWindo
SetWindowText(nHandle, "Hola que tal"
// imported GetD
nContexto = GetDC(nHandle
Excuse me .. what do you mean by 'Google' .. :-) .. (sorry my english is very poor)

Finally - sorry so many words :-) - .. I think that could be some refresh or WM_PAINT .. problem ..

Thanks again

Roberto
 
Thanks Alex ..
handle

:-) .. that's rigth man .. and I 'feel', here is the problem, but I don't see exactly wich one

plis see my long answer to Chris, if you wish

Thanks again

Roberto
 
First off, if I understand you correct you don't get an exception when
P/Invoking to the function Prueba inside the DLL and you do see the first
MessageBox, right? So at least you are entering the native function without
problems.

However, I do see some issues in your DLL. Instead of testing variables in
if statements you are assigning new values to them. For instance, what you
are doing is the following:

if (hWnd = NULL) etc.

The result of this statement is that you assign hWnd to NULL, so you are
overwriting a valid Window Handle with NULL.

Instead you should have used the logical operator ==.

if (hWnd == NULL) etc.

This is true for all your if statements.

--
Regards,

Maarten Struys, eMVP
PTS Software bv

www.opennetcf.org | www.dotnetfordevices.com

AlaSoft said:
Thanks !


I think yes .. see the following

#include "stdafx.h"

BOOL APIENTRY DllMain (HANDLE hModule, DWORD
ul_reason_for_call, LPVOID lpReserved) {
return TRUE;
}

extern "C" __declspec(dllexport) int __stdcall Prueba(HWND hWnd)
{
HDC hDc;
HWND hWndDos;
BOOL nSalida;

MessageBox(NULL,_T("Entrada"),_T("Atención"),MB_OK);
if (hWnd=NULL)
{MessageBox(NULL,_T("ERROR: Manejador de Ventana Nulo"),_T("Atención"),MB_OK);}
hDc=GetWindowDC(hWnd);
if (hDc=NULL)
{MessageBox(hWnd,_T("ERROR: Contexto Nulo"),_T("Atención"),MB_OK);}
nSalida=ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4, NULL);
if (nSalida=0)
{MessageBox(hWnd,_T("ERROR: No se desplego el literal"),_T("Atención"),MB_OK);}
SetWindowText(hWnd,_T("PROCESO EN MARCHA"));
ReleaseDC(hWnd,hDc);
hWndDos=FindWindow(NULL,_T("Prueba"));
return (int)hWndDos;

}

Now the point here is .. that this code seems to work .. but again, it
doesn't display anything. See that SetWindowText(...) ? ..
well inside the .dll, doesn't change the caption ... outside - invoked in
VB.Net as a lonely call - yes ! ..
Note: The MessageBox(..) inside the .dll .. works fine ! .. - it shows
just the first, because there are no errors (hDC=Null, etc) -
 
. Instead of testing variables i
if statements you are assigning new values to them .

:-) .. you are quite rigth ! .. Now it's working ! .. thank you all of you - even in Emulator

Robert

----- Maarten Struys, eMVP wrote: ----

First off, if I understand you correct you don't get an exception whe
P/Invoking to the function Prueba inside the DLL and you do see the firs
MessageBox, right? So at least you are entering the native function withou
problems

However, I do see some issues in your DLL. Instead of testing variables i
if statements you are assigning new values to them. For instance, what yo
are doing is the following

if (hWnd = NULL) etc

The result of this statement is that you assign hWnd to NULL, so you ar
overwriting a valid Window Handle with NULL

Instead you should have used the logical operator ==

if (hWnd == NULL) etc

This is true for all your if statements

--
Regards

Maarten Struys, eMV
PTS Software b

www.opennetcf.org | www.dotnetfordevices.co

AlaSoft said:
ul_reason_for_call, LPVOID lpReserved)
return TRUE


HDC hDc
HWND hWndDos
BOOL nSalida
if (hWnd=NULL
{MessageBox(NULL,_T("ERROR: Manejador de Ventan Nulo"),_T("Atención"),MB_OK);
hDc=GetWindowDC(hWnd)
if (hDc=NULL
{MessageBox(hWnd,_T("ERROR: Contexto Nulo"),_T("Atención"),MB_OK);
nSalida=ExtTextOut (hDc, 10, 10, ETO_OPAQUE, NULL, _T("Hi World !"), 4 NULL)
if (nSalida=0
{MessageBox(hWnd,_T("ERROR: No se desplego e literal"),_T("Atención"),MB_OK);
SetWindowText(hWnd,_T("PROCESO EN MARCHA"))
ReleaseDC(hWnd,hDc)
hWndDos=FindWindow(NULL,_T("Prueba"))
return (int)hWndDos
doesn't display anything. See that SetWindowText(...) ? .
well inside the .dll, doesn't change the caption ... outside - invoked i
VB.Net as a lonely call - yes ! .
 
Back
Top