Solved

Invalid parameter in DLL function call from VB6

Posted on 2013-06-04
13
1,031 Views
Last Modified: 2013-07-02
Hi I have a third party dll written in Microsoft C and I want to use one of its function within my vb6 code. The creator of dll has give it to me the data structure that the dll needs and the declaration of the function to call it from my vb6 code. But it gives me back an error about invalid parameter.
The data structure:
Public Type ctcAssignData
    deviceType As Integer
    APIversion As String * 1
    APIextensions As String * 1
    deviceDN As String * 24
End Type

Open in new window

The Function declaration for use in vb6:
Declare Function ctcAssign Lib "ctcapi32.dll" (channel As Long, struct As ctcAssignData, ByVal server As String, ByVal logid As String, ByVal network As String) As Long

Open in new window


The vb6 code I made to use the dll function:
Private Sub Command1_Click()
    Dim channel As Long
    Dim struct As ctcAssignData
    Dim CallID As Long
    Dim status As Long
    struct.deviceDN = "322"
    struct.deviceType = 200
    struct.APIversion =&H40 
    struct.APIextensions = 2    
    status = ctcAssign(channel, struct, "servername", "server_logical_name", "ncacn_ip_tcp")
End Sub

Open in new window


It throws me a status=1018 that according to dll's creator is that I have an invalid parameter!

Thanks
0
Comment
Question by:nonlinearly
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 500 total points
ID: 39219019
This particular dll call is just a touch more complex than the ones I've worked with (specifically the fixed length string in the structure), but I'll take a stab at what might be wrong.

Try changing APIextensions from 2 to "2".

Try changing the size of the strings to the size of the fixed length string
i.e. try "322                        " (but I don't think this is it).

Try ending the string with vbNullChar (i.e. "322" & vbNullChar).  This makes sure the string ends with '\0' that C expects at the end of strings.

The other possibility is the fact that in VB, strings are Unicode.  For the parameters like 'logid' and 'network', because the strings are passed 'byval', VB will automatically convert the unicode string to an ascii string and pass a pointer of the ascii string to the dll.  But I'm not sure what it will do regarding the fixed length strings of the structure.  So you might need to replace the fixed length strings with character arrays.  Then fill in the values of the various characters.

In any case, it would be helpful if the C programmer could attempt to debug the code and see what is getting passed to C and specify exactly what is wrong.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39219433
i would assume that you forgot to pass a valid 'channel'.

Sara
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39220289
i would assume that you forgot to pass a valid 'channel'.
Is zero (0)  a valid channel?  
Since you never assigned the channel variable a value, VB will default it for you to zero.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:lomo74
ID: 39221311
Shouldn't it be
Declare Function ctcAssign Lib "ctcapi32.dll" (channel As Long, ByRef struct As ctcAssignData, ByVal server As String, ByVal logid As String, ByVal network As String) As Long
0
 

Author Comment

by:nonlinearly
ID: 39221374
1. It not necessary to set ByRef. If there is not anything then it is ByRef in vb6.
2. The initial value of the channel argument does not care us. The channel variable because it is ByRef it will takes a value from the Function and then we will using it in the rest of the program. It maps a C pointer.
3. I try all of first comment but nothing happened.
4. The dll is a third party component from dialogic that does not exist anymore to support it.
5 I have attached the dll documentation while it is for C use. But maybe someone find it useful. The function is ctcAssign. Also I have attached and C example of using this ctcAssign function. The C example works.
ctcv50prog.pdf
ctc-exp.c
0
 
LVL 8

Expert Comment

by:lomo74
ID: 39221404
1. are you sure? I'm not a VB guy but I was sure of the opposite... :)
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 39221878
  status = ctcAssign(channel, struct, "servername", "server_logical_name", "ncacn_ip_tcp")

   I think part of the problem might be the way you have used the string parameters in the function call.
   You have just used the string in the API call, which means it is just on the stack rather than an address space within your VB program. When crossing language boundaries there can be problems. I would suggest declaring a string variable called 'server' and populating it with the value "servername" and then passing the reference properly in the API call. Just passing the value in the API call hides a lot of behaviour. After all, how do you know what it is being converted into when passed to the DLL? A string?, a varchar, binary char, and so on.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39222624
 I think part of the problem might be the way you have used the string parameters in the function call.

That's a red herring.  Because the strings are declared as "ByVal", that means that VB takes the orginal UNICODE string and converts it to an ASCII string, and a pointer to the ASCII string gets passed to the DLL.  So changing the location of the UNICODE string will have no effect on what gets passed to the DLL.
0
 
LVL 8

Expert Comment

by:lomo74
ID: 39222689
what about putting together a fake C DLL which exposes the same interface as the real DLL, but does nothing but dumping the parameters that receives.
thus testing if the problem lies in the parameter passing.
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39222727
I did a test, wrote a dll function that took similar parameters, then called that dll with a copy of your code and looked in the debugger to see what the C code sees.

The struct on the C side looks like this:
deviceType=200
APIVersion = '6'
APIextension = '2'
deviceDN = "322                     ..." where ... is binary garbage (because the string isn't NULL terminated).

Because you haven't include some .h files, I don't know if what you've passed for deviceType, APIVersion, and APIextension are correct.

But what I'm feeling pretty sure of is that the deviceDN is a likely culprit if everything else is correct.

The reason is because strings are different in the VB and C world.  In VB, a string is an object that contains a memory address (where the string is stored) and a value indicateing the length of the string.  But in the C world, a string is simply a pointer to a memory address that contains the 1st character of the string.  The string ends when a NULL character (binary zero) is encountered.

But because deviceDN is defined as a fixed length string, when you assign deviceDN="322", it actually gets populated with "322" followed by 21 spaces.  Additionally, because this is a fixed length string, the ASCII conversion to pass to the DLL does NOT append a NULL terminator.  The result is that the value for deviceDN as seen in the C world never ends (until a random NULL is encountered in memory).

So what you need to do is modify the code assign that value like this:

deviceDN = "322" & vbNullChar

That way, what gets passed to C is "322\0                    " (where \0 represents NULL).  The C code with interpret that as the string "322".

The other two fixed length strings are of length 1, so they are really just BYTE values passed to the C dll.  So it makes sense that "2" gets transmitted to C as the ASCII character "2".  I'm not sure why '&H40' is getting transmitted to C as the ASCII character "6", because the ASCII character associated with the hex value 0x40 is the ASCII character "@".  So that could also be a source of your trouble.
0
 

Author Comment

by:nonlinearly
ID: 39232673
I have attached the C header files (.h) and a .bas file (vb6) that has all the declarations, structures and constants about the use of the dll in vb.
But as I have mentioned before I tried the vbNullChar with no effect!
To be honest the values of the struct are not:
struct.deviceType = 200
struct.APIversion =&H40
struct.APIextensions = 2

but instead of literal values I use the constants corresponding to these values. These constants declarations are in the .bas file.
struct.deviceType = ctcK_Dn
struct.APIversion =ctcK_CTCV40
struct.APIextensions = ctcK_CstaPrivate

However I think that this does not affect the whole story!
Thanks
header-files.zip
ctcdef.bas
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 39235378
I've found the problem.  You need to change the declaration of the type to use Byte rather than String * 1

Public Type ctcAssignData
    deviceType As Integer
    APIversion As Byte
    APIextensions As Byte
    deviceDN As String * 24
End Type

Open in new window

The problem is that these variables are defined as 'unsigned char' in the C code, with the constants defined as binary values (i.e. ctcK_CstaPrivate = 2).  But because VB defines APIextension as 'String *1', the assignment of a number (2) into a string results in a string conversion (so the ASCII character for '2' gets placed in APIextension rather than the binary value 2).

One other thing (which might not be a problem in your 'real' code) is that the string parameters of ctcAssign are defined in the C code as character arrays of length 16.  But your sample parameter ("server_logical_name") is more than 16 characters.  To avoid errors because you pass in strings that are too long, you MIGHT need to change the calling function to look something like this:
Dim server as String * 16
Dim login as String * 16
Dim network as String * 16
server = "servername"
login = "server_logical_name" 'Only "server_logical_c" will get copied
network = "ncacn_ip_tcp"
status = ctcAssign(channel, struct, server, logid,network)

Open in new window

0
 

Author Closing Comment

by:nonlinearly
ID: 39292643
HooKooDooku are awesome. The problem was in two points.
1. add vbNullChar in deviceDN
2. Change to byte the APIversion and APIextensions

THANKS A LOT...
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question