Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,416 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,380 people online right now. Registration is fast and FREE... Join Now!




Problem with passing string from VB6 to C DLL

 
Reply to this topicStart new topic

Problem with passing string from VB6 to C DLL

Servo Wizard
14 Oct, 2008 - 03:03 PM
Post #1

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 11


My Contributions
Hi,

My problem is with passing "\\\\.\\readios" from VB6 to C DLL. I can call the Function from VB6 and it returns the file handle, but only if I supply the data in the DLL code.

I need to replace *The Problem* in the C code with a declared variable that will accomplish the same as the ' const char* ' declaration in the code. Replacing the *The Problem* with the ' const char* ' declaration does not solve the problem.

Thanks in advance for helping,
Servo

Visual Basic 6.0:

CODE
Option Explicit

Private Declare Function GetDriverHandle _
        Lib "driverid.dll" ( _
        ByVal ServiceName As String) As Long

Private Sub Form_Load()
        Dim Service As String

        Service = "\\\\.\\readios"

        With DRO
            .Text = GetDriverHandle(Service)
            .SelStart = 0
            .SelLength = Len(.Text)
            .SelColor = &HFF00&
            .SelLength = 0
        End With
End Sub


Visual C 6.0:

CODE
#include <stdio.h>
#include <windows.h>

BOOL APIENTRY DllMain(HANDLE hModule,DWORD dwMission,LPVOID lpReserved)
{
switch (dwMission)
       {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH: break;
       }

return TRUE;
}

HANDLE hFile;

HANDLE __stdcall GetDriverHandle(*The Problem*)
{
const char* ServiceName="\\\\.\\readios";

hFile=CreateFile
      (
       ServiceName,
       GENERIC_READ,
       FILE_SHARE_READ,
       NULL,
       OPEN_EXISTING,
       FILE_ATTRIBUTE_NORMAL,
       NULL
      );

return hFile;
}


User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Problem With Passing String From VB6 To C DLL
14 Oct, 2008 - 08:21 PM
Post #2

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Post this in either the Visual Basic or C++ / C forum
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Problem With Passing String From VB6 To C DLL
14 Oct, 2008 - 08:32 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to Visual Basic smile.gif
User is online!Profile CardPM
+Quote Post

Servo Wizard
RE: Problem With Passing String From VB6 To C DLL
15 Oct, 2008 - 05:32 AM
Post #4

New D.I.C Head
*

Joined: 12 Jul, 2007
Posts: 11


My Contributions
PsychoCoder,

Did you read the entire post before you made the decision to move it to the Visual Basic forum?

*The Problem* is in the C code!

Servo
User is offlineProfile CardPM
+Quote Post

BigThoughts
RE: Problem With Passing String From VB6 To C DLL
16 Oct, 2008 - 06:52 PM
Post #5

New D.I.C Head
*

Joined: 16 Oct, 2008
Posts: 4


My Contributions
The problem is with the argument list:

You have this:

HANDLE __stdcall GetDriverHandle(*The Problem*)

or, perhaps, this:

HANDLE __stdcall GetDriverHandle(const char ServiceName)

Either way, your C function expects a string to be passed by value (ByVal). However, when VB passes a string, regardless of whether the string is a literal or the value of a variable, what actually gets passed is a pointer to the beginning of the string.

You should have this.

HANDLE __stdcall GetDriverHandle(LPCTSTR ServiceName)

HTH.

David Gray
Irving, Texas, USA


QUOTE(Servo Wizard @ 14 Oct, 2008 - 04:03 PM) *

Hi,

My problem is with passing "\\\\.\\readios" from VB6 to C DLL. I can call the Function from VB6 and it returns the file handle, but only if I supply the data in the DLL code.

I need to replace *The Problem* in the C code with a declared variable that will accomplish the same as the ' const char* ' declaration in the code. Replacing the *The Problem* with the ' const char* ' declaration does not solve the problem.

Thanks in advance for helping,
Servo

Visual Basic 6.0:

CODE
Option Explicit

Private Declare Function GetDriverHandle _
        Lib "driverid.dll" ( _
        ByVal ServiceName As String) As Long

Private Sub Form_Load()
        Dim Service As String

        Service = "\\\\.\\readios"

        With DRO
            .Text = GetDriverHandle(Service)
            .SelStart = 0
            .SelLength = Len(.Text)
            .SelColor = &HFF00&
            .SelLength = 0
        End With
End Sub


Visual C 6.0:

CODE
#include <stdio.h>
#include <windows.h>

BOOL APIENTRY DllMain(HANDLE hModule,DWORD dwMission,LPVOID lpReserved)
{
switch (dwMission)
       {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH: break;
       }

return TRUE;
}

HANDLE hFile;

HANDLE __stdcall GetDriverHandle(*The Problem*)
{
const char* ServiceName="\\\\.\\readios";

hFile=CreateFile
      (
       ServiceName,
       GENERIC_READ,
       FILE_SHARE_READ,
       NULL,
       OPEN_EXISTING,
       FILE_ATTRIBUTE_NORMAL,
       NULL
      );

return hFile;
}



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:55PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month