Click here to Skip to main content
15,914,222 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 3:37
sitebuilderLuc Pattyn29-Jun-10 3:37 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 3:43
Hrizip29-Jun-10 3:43 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
freakyit29-Jun-10 4:00
freakyit29-Jun-10 4:00 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 4:31
Hrizip29-Jun-10 4:31 
AnswerRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 4:34
sitebuilderLuc Pattyn29-Jun-10 4:34 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 4:36
Hrizip29-Jun-10 4:36 
AnswerRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 7:41
sitebuilderLuc Pattyn29-Jun-10 7:41 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 7:58
Hrizip29-Jun-10 7:58 
In the meantime I also tried it without SetLastError and Charset.Auto, and I toyed around with byval/byref, nothing changed.
I also did try it with printerType set to both zero and 330 , with same result.

I have found someone with a similar problem, unfortuantely the said individual does his programming in delphi so I am still trying to understand what he said, heres the text:

I am attempting to access a function in a C-style DLL, written in Microsoft Visual C++ version 6.0.

The function is defined as follows:

INT __stdcall ZBRGetHandle(LPHANDLE hPrinter, LPSTR pName, LPINT printerType, LPINT err)

In my Delphi project, I prototype the function as:

function ZBRGetHandle(var handle: THandle; var drvName: Array of Byte; var prnType: Integer; var err: Integer) : Integer;
{$IFDEF WIN32} stdcall; {$ENDIF} external 'ZBRPrinter.dll' ;

I have a wrapper function within a class that calls this function. The class method is defined as:
function Open(var drvName: array of Byte; var errValue: Integer) : Integer; stdcall;

I call it as:
function TZBRPrinter.Open(var drvName: Array of Byte; var errValue: Integer) : Integer; stdcall;
begin
Result := ZBRGetHandle(_handle, drvName, _prnType, errValue);
end;

note: _handle & _prnType are defined within the class TZBRPrinter as:
private
_handle: THandle; // _handle to printer driver
_prnType: Integer;

I initialize both variables in the class constructor:
constructor TZBRPrinter.Create;
begin
// Execute the parent (TObject) constructor first
inherited; // Call the parent Create method

// Now set defaults
self._handle := 0;
self._prnType := 100;
end;

errValue is set to -1 prior to the call to TZBRPrinter.Open.

In an attempt to root-cause the problem, I used my delphi executable as the debugging application for my ZBRPrinter.dll project.

When the ZBRGetHandle is called via the Delphi application, what I find is puzzling:

With respect to ZBRGetHandle within the dll source code, I find that the 3rd parameter, LPINT printerType, is invalid, and
the 4th parameter, LPINT err is set to 100. The first 2 parameters are correct.

I have tried using a fixed array of bytes and chars as the second parameter, and a String, yet I find that I still encounter problems when passing all 4 parameters to this DLL function. I find that if the dll's second parameter only gets the first char of the string, then the 3rd parameter gets the value 100, and the 4th gets the value -1 as I intend. If the second parameter contains the entire string I am passing, then the 3rd parameter is corrupted and the 4th parameter contains the value 100, which is intended for the 3rd parameter.

Can someone explain to me how I pass a null-terminated string as the second parameter to my dll such that it doesn't cause the 3rd parameter to become corrupted and the 4th to be assigned the 3rd's intended value?

I had actually figured out the problem yesterday.

I was already making sure to null terminate the strings I am using. However, I did not realize that Delphi wouldn't pass the address of the byte array to the c-style function even when I specifically declared the function parameter as a reference.

What I did to solve the problem was to delcare a PChar and assign it the address of the dynamic byte array. I also changed my function to use a PChar instead of a reference to a byte array.

Once I made these changes, I was able to successfully pass the null terminated string to my c-style function and the function call succeeded.


GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 8:26
sitebuilderLuc Pattyn29-Jun-10 8:26 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 8:34
Hrizip29-Jun-10 8:34 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 8:52
sitebuilderLuc Pattyn29-Jun-10 8:52 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 9:12
Hrizip29-Jun-10 9:12 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 10:03
sitebuilderLuc Pattyn29-Jun-10 10:03 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 10:17
Hrizip29-Jun-10 10:17 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Luc Pattyn29-Jun-10 10:26
sitebuilderLuc Pattyn29-Jun-10 10:26 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 10:29
Hrizip29-Jun-10 10:29 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip29-Jun-10 8:17
Hrizip29-Jun-10 8:17 
GeneralRe: Pinvoking a non WIN32 API under VB, cant get handle to printer Pin
Hrizip30-Jun-10 23:28
Hrizip30-Jun-10 23:28 
QuestionMDI window application with treeview by visual basic 2005 Pin
Andraw Tang28-Jun-10 9:03
Andraw Tang28-Jun-10 9:03 
AnswerRe: MDI window application with treeview by visual basic 2005 Pin
Dave Kreskowiak28-Jun-10 10:46
mveDave Kreskowiak28-Jun-10 10:46 
GeneralRe: MDI window application with treeview by visual basic 2005 Pin
Andraw Tang28-Jun-10 11:20
Andraw Tang28-Jun-10 11:20 
GeneralRe: MDI window application with treeview by visual basic 2005 Pin
Dave Kreskowiak28-Jun-10 14:09
mveDave Kreskowiak28-Jun-10 14:09 
GeneralRe: MDI window application with treeview by visual basic 2005 Pin
Andraw Tang28-Jun-10 11:39
Andraw Tang28-Jun-10 11:39 
GeneralRe: MDI window application with treeview by visual basic 2005 Pin
Dave Kreskowiak28-Jun-10 14:23
mveDave Kreskowiak28-Jun-10 14:23 
GeneralRe: MDI window application with treeview by visual basic 2005 Pin
Andraw Tang29-Jun-10 3:18
Andraw Tang29-Jun-10 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.