# Calling a Function in C DLL from VB.net



## gagan8877 (Nov 12, 2005)

Hi All

I need to use an external C DLL (which I did not write) from VB.net Code. I am trying to include certain functionality in the User Interface (UI) of my application from this compiled 3rd party DLL. The functions defined in the DLL are as follows (from product documentation):

1) DSGetProjectList

Obtains a list of all projects on the host system

Syntax 
char* DSGetProjectList(void);

Return Values 
If the function succeeds, the return value is a pointer to a series of null-terminated strings, one for each project on the host system, ending with a second null character. The following example shows the buffer contents with <null> representing the terminating null character: 
project1<null>project2<null><null>

If the function fails, the return value is NULL.

2) DSOpenProject

Opens a project.

Syntax 
DSPROJECT DSOpenProject 
(char *ProjectName);

Parameter 
ProjectName is a pointer to a null-terminated string that specifies the 
name of the project to open.

Return Values 
If the function succeeds, the return value is a handle to the project. 
If the function fails, the return value is NULL. Use DSGetLastError to 
retrieve one of the following:

Remarks 
The DSGetProjectList function can return the name of a project that 
does not contain valid DataStage jobs, but this is detected when 
DSOpenProject is called. A process can only have one project open 
at a time. 
Token Description 
DSJE_BAD_VERSION The DataStage server is an older version than the DataStage API. 
DSJE_INCOMPATIBLE_SERVER The DataStage Server is either older or newer than that supported by this version of DataStage API. 
DSJE_SERVER_ERROR Internal error. DataStage Server returned invalid data. 
DSJE_BADPROJECT Invalid project name. 
DSJE_NO_DATASTAGE DataStage is not correctly installed on the server system.

------------WHAT I DID------------

1. I Copied the DLL file in the VB Project folder and added a Module to the Project.

2. Declared the first Function in the module:

Declare Function DSGetProjectList Lib "vmdsapi" ()

3. I read something on a website about adding .def Files to the project. But I could not find how to add the Module Definition File - I only have VB.Net installed not complete Visual Studio.Net and file is a VC++ object - not sure.

So I created a text file in the project folder with the same name as dll file and renamed the extension to .def extension. Added the file as an existing item to the VB.net project.

4. The .def file looks as under:

LIBRARY vmdsapi 
DESCRIPTION 'A C++ dll that can be called from VB'

EXPORTS 
DSGetProjectList

5. Created a Button to test:

Dim lRetVal As Integer 
lRetVal = DSGetProjectList ()

6. It fails and I get:

Unhandled Exception: System.Runtime.InteropServices.MarshalDirectiveExceptionInvoke restriction: can not return variants

7. The first function does NOT require an parameters and the second DOES, I need to call them both.

8. Please help me if u know how to call them both (with and without parameters)

- Help will be appreciated


----------

