4 Replies - 2837 Views - Last Post: 16 October 2008 - 07:52 PM Rate Topic: -----

#1 Servo Wizard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-July 07

Problem with passing string from VB6 to C DLL

Posted 14 October 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:

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:

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with passing string from VB6 to C DLL

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Problem with passing string from VB6 to C DLL

Posted 14 October 2008 - 09:21 PM

Post this in either the Visual Basic or C++ / C forum
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Problem with passing string from VB6 to C DLL

Posted 14 October 2008 - 09:32 PM

Moved to Visual Basic :)
Was This Post Helpful? 0
  • +
  • -

#4 Servo Wizard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-July 07

Re: Problem with passing string from VB6 to C DLL

Posted 15 October 2008 - 06:32 AM

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
Was This Post Helpful? 0
  • +
  • -

#5 BigThoughts  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-October 08

Re: Problem with passing string from VB6 to C DLL

Posted 16 October 2008 - 07:52 PM

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


View PostServo Wizard, on 14 Oct, 2008 - 04:03 PM, said:

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:

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:

#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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1