| |

VerySource

 Forgot password?
 Register
Search
View: 904|Reply: 9

Problems calling DLLs written in C

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-14 09:30:01
| Show all posts |Read mode
'typedef struct _state_now {
'unsigned char ifTel: 1;
'unsigned char ifRing: 1;
'unsigned char ifError: 1;
'unsigned char ifDtmf: 1;
'unsigned char dtmf: 4;
'unsigned char hd;
'} STATE_NOW;

'Function: query status
'Entry: 1 port, port number (0-7 corresponds to 64-71 of USB port), state
'Exit: 1 succeeded; 0 failed.
'int WINAPI GetKHTState (int port, STATE_NOW * state)

Others wrote in VB
Declare Sub GetKHTState Lib "mtudll.DLL" (ByVal port As Long, ByRef state As Integer)


The code I call with DELPHI is as follows
 type
  STATE_NOW = packed record
    ifTel: char;
    ifRing: char;
    ifError: char;
    ifDtmf: char;
    dtmf: array [1..4] of char;
    hd: pchar;
  end;
  PSTATE_NOW = ^ STATE_NOW;

function GetKHTState (port: integer; state: PSTATE_NOW): integer; stdcall; external 'mtudll.DLL' name 'GetKHTState';

Var
st: pSTATE_NOW;
begin
if GetKHTState (64, st) = 1 then // true when executed here, indicating successful return
   edit1.Text: = st.ifTel // but did not get any data, it is empty

I was wrong there, what should I do?
Very anxious, help?
Reply

Use magic Report

0

Threads

14

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-10 22:00:01
| Show all posts
Var
st:pSTATE_NOW;
begin
new( st );
if GetKHTState(64,st)=1 then //true when executed here, indicating successful return
edit1.Text :=st.ifTel //But no data is obtained, it is empty
dispose( st );
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-14 08:30:01
| Show all posts
Try again today without error
But still no data is returned, or null value, I really don’t know what to do
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-14 09:15:01
| Show all posts
'typedef struct _state_now{
'unsigned char ifTel:1;
'unsigned char ifRing:1;
'unsigned char ifError:1;
'unsigned char ifDtmf:1;
'unsigned char dtmf:4;
'unsigned char hd;
'} STATE_NOW;

type
  STATE_NOW=packed record
    ifTel:char;
    ifRing:char;
    ifError:char;
    ifDtmf:char;
    dtmf:array[1..4] of char;
    hd:char; //hd:pchar; change it here
  end;
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-14 13:15:01
| Show all posts
Tried
Or not
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-14 18:15:01
| Show all posts
There is no C/C++ bit field in Delphi (VB is the same, so it uses integer)
Since PSTATE_NOW is a pointer in C, it should be defined like this in delphi
function GetKHTState(port:integer; var state:word):integer;stdcall; external'mtudll.DLL' name'GetKHTState';
Then, use state and shr separately to get the value of each state
var
  state:word;
  i:integer;
  ifTel,ifRing,ifError,ifDtmf,dtmf,hd:integer;
begin
  i:=GetKHTState(1,state);
  Edit1.Text:=inttostr(i);
  ifTel:=state and 1;
  ifRing:=(state shr 1) and 1;
  ifError:=(state shr 2) and 1;
  ifDtmf:=(state shr 3) and 1;
  dtmf:=(state shr 4) and $f;
  hd:=(state shr 8) and $ff;
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-15 09:45:01
| Show all posts
The problem doesn't seem to be here

ufoprc
   There is no C/C++ bit field in Delphi (VB is the same, so it uses integer)
Since PSTATE_NOW is a pointer in C, it should be defined like this in delphi
function GetKHTState(port:integer; var state:word):integer;stdcall; external'mtudll.DLL' name'GetKHTState';
Then, use state and shr separately to get the value of each state
var
  state:word;
  i:integer;
  ifTel,ifRing,ifError,ifDtmf,dtmf,hd:integer;
begin
  i:=GetKHTState(1,state);
  Edit1.Text:=inttostr(i);
  ifTel:=state and 1;
  ifRing:=(state shr 1) and 1;
  ifError:=(state shr 2) and 1;
  ifDtmf:=(state shr 3) and 1;
  dtmf:=(state shr 4) and $f;
  hd:=(state shr 8) and $ff;

tried
All bits are 0, because it is an integer, the initial is 0, so nothing is actually returned

The program written with the VB they give can get an integer of 4098, and a binary of 1000000000010
Is ifRing bit is 1

Why can't I get the return number with DELPHI?
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-20 15:00:01
| Show all posts
I made a test program, it should be possible, note that the state must be defined as var state:word
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-20 21:00:02
| Show all posts
Try edit1.Text :=inttostr(st.ifTel);
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-24 10:00:01
| Show all posts
I also defined it as var state:word
Problem is not solved
But thank you for your kind help.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list