[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1536
  • Last Modified:

File Sending with IrDA (PPC to Others) with VB.NET

I am currenting using "OBEX" as the servicename to send a image from a PPC to Any other device, e.g PC thru IrDA.

Current my code allows me to send the file, but it doesn't appear anywhere in my PC. (It only appears to send, not sure if it really did.)

Also, while sometimes it's ok to send 1 file, it crashes when i try to send another.

Error given was

"An unhandled exception of type 'System.IO.IOException' occurred in System.dll, Additional information: Unable to write data to the transport connection."

My IrDA Data transfer code is as followed...

Private Function ir_senddata() As Boolean
    Dim irClient As New IrDAClient
    Dim irServiceName As String = "OBEX"
    Dim buffersize As Integer = 65535

    Dim fs As Stream
    Try
      fs = New FileStream("\TEST.JPG", FileMode.Open)
    Catch exFile As Exception
      global.prompt(global.errorcode(5) & exFile.ToString) : Return False
    End Try

    Try
      irClient = New IrDAClient(irServiceName)
    Catch exS As SocketException
      global.prompt(global.errorcode(6) & exS.Message) : Return False
    End Try

    Dim baseStream As Stream = irClient.GetStream()
    Dim length As Byte() = BitConverter.GetBytes(Fix(fs.Length))
    baseStream.Write(length, 0, length.Length)

    Dim buffer(buffersize) As Byte
    Dim fileLength As Integer = CInt(fs.Length)

    While fileLength > 0
      Dim numRead As Integer = fs.Read(buffer, 0, buffer.Length)
      baseStream.Write(buffer, 0, numRead)
      fileLength -= numRead
    End While

    fs.Close()
    baseStream.Close()
    irClient.Close()
  End Function


Can anyone divert me to a really good example code of file/data sending with IrDA from PPC in VB.NET? And what caused the error? (And, any Bluetooth codes as well?:))

Thanks!
0
rahjinoh
Asked:
rahjinoh
  • 21
  • 18
1 Solution
 
rahjinohAuthor Commented:
If you noticed, that's the code i based on in my function, i want to send it to my notebook thru IrDA, however, i would like to "just" send it, without having to have a "Server Listener" to wait for a IrDA connection to receive the file.

I would like to just send it, and Windows can pick up the file directly.

The sample from gotdotnet provides codes for a Sender & Receiver. My code is based on the Sender. But is there a way to send my file to another device without a "server"?
0
 
Mikal613Commented:
you would need a way to interpret the beam signals from the IR port
0
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
rahjinohAuthor Commented:
Well, after so long but several unsuccessful attempts, here's to savage the situation :)

Is there anyway to call up the BEAM FILES form that appears when you tap and hold a file and select BEAM FILE in File Explorer in WM2003?

If there's a way to call up that and allow me to send file thru that, everything would be ok.

Anyone? :)
0
 
Mikal613Commented:
does he still want an answer?
0
 
rahjinohAuthor Commented:
If you have an answer for me, feel free to say it, coz i've sort of neglected that project due to this problem, haven't been able to solve it.

Question, in Resco Explorer, or File Explorer, you can tap SEND TO -> INFRARED to send files.
How do you call up that exact form to send files thru that?

Any ideas?
0
 
Mikal613Commented:
Option Explicit
Public Declare Function CreateProcess Lib "coredll" Alias "CreateProcessW" ( _
    ByVal pszImageName As String, _
    ByVal pszCmdLine As String, _
    ByVal psaProcess As Long, _
    ByVal psaThread As Long, _
    ByVal fInheritHandles As Long, _
    ByVal fdwCreate As Long, _
    ByVal pvEnvironment As Long, _
    ByVal pszCurDir As Long, _
    ByVal psiStartInfo As Long, _
    ByVal pProcInfo As Long) As Long

Private Sub Command1_Click()
    Dim lnCreateProcess As Long
    Dim strFileName As String
    strFileName = "\My Documents\DocName.psw" <---- The File you wanna beam
    On Error Resume Next
    lnCreateProcess = CreateProcess("\Windows\Beam.exe", strFileName, 0, 0, 0, 0, 0, 0, 0, 0)
    If lnCreateProcess = 0 Then
        MsgBox Err.Description
    End If
End Sub
0
 
rahjinohAuthor Commented:
I've got a NotSupportedException. Any ideas?
If it would help, i'm using a XDAII

Thanks for your patience :)
0
 
Mikal613Commented:
you have to use DllImport to make the API work
0
 
rahjinohAuthor Commented:
<DllImport("CORE.DLL")>  ?

Any hints? i'm really dumb at marshalling. Which unmanaged dll should i import?
0
 
Mikal613Commented:
<DllImport("coredll")> _

    Public Declare Function CreateProcess Lib "coredll" Alias "CreateProcessW" ( _
    ByVal pszImageName As String, _
    ByVal pszCmdLine As String, _
    ByVal psaProcess As Long, _
    ByVal psaThread As Long, _
    ByVal fInheritHandles As Long, _
    ByVal fdwCreate As Long, _
    ByVal pvEnvironment As Long, _
    ByVal pszCurDir As Long, _
    ByVal psiStartInfo As Long, _
    ByVal pProcInfo As Long) As Long

0
 
rahjinohAuthor Commented:
'System.Runtime.Interopservices.DllimportAttribute' cannot be applied to a 'Declare'
0
 
Mikal613Commented:
try public shared instead of Public declare
0
 
rahjinohAuthor Commented:
I tried that before too, there's a End Of Statement Expected Error, but i dun understand why.
0
 
Mikal613Commented:
Imports System.Runtime.InteropServices

    Imports System.Text

 
the import should be global
 

        <DllImport("coredll")> _

                Public Shared Function CreateProcess Lib "coredll" Alias "CreateProcessW" ( _
    ByVal pszImageName As String, _
    ByVal pszCmdLine As String, _
    ByVal psaProcess As Long, _
    ByVal psaThread As Long, _
    ByVal fInheritHandles As Long, _
    ByVal fdwCreate As Long, _
    ByVal pvEnvironment As Long, _
    ByVal pszCurDir As Long, _
    ByVal psiStartInfo As Long, _
    ByVal pProcInfo As Long) As Long
0
 
rahjinohAuthor Commented:
i still get the End of Statement Expected error though
0
 
rahjinohAuthor Commented:
Adding a "End Function" didn't help too
0
 
Mikal613Commented:
Public Shared Function CreateProcess Lib "coredll" Alias "CreateProcessW" ( _
    ByVal pszImageName As String, _
    ByVal pszCmdLine As String, _
    ByVal psaProcess As Long, _
    ByVal psaThread As Long, _
    ByVal fInheritHandles As Long, _
    ByVal fdwCreate As Long, _
    ByVal pvEnvironment As Long, _
    ByVal pszCurDir As Long, _
    ByVal psiStartInfo As Long, _
    ByVal pProcInfo As Long) As Long

END FUNCTION <------ thats the problem
0
 
rahjinohAuthor Commented:
As i mentioned above, adding the "End Function" didn't help too :)
0
 
Mikal613Commented:
what line is the error?
0
 
rahjinohAuthor Commented:
the blue wavy error underline started from

Lib "coredll".... all the way to ....ByVal pProcInfo As Long)As Long

with an Error : End Of Statement Expected
0
 
Mikal613Commented:
http://www.devx.com/wireless/Article/21291

check out the code whats different?
0
 
Mikal613Commented:
can you post all your code?
0
 
rahjinohAuthor Commented:
Removing Lib "coredll" Alias "CreateProcessW" removes the error, but still gives NotSupportedException in my PPC.

i created a new form with a btn and used your code with the Command1_Click on top. On change made was the Path and File name which is now "\Storage Card\image.jpg"
0
 
rahjinohAuthor Commented:
From what i can tell, the code actually emulates a "Shell" function that doesn't exists in CF .NET.
But i can't get it to work.
0
 
Mikal613Commented:
Imports System.Runtime.InteropServices
Imports System.Text



Class SHELLEXECUTEEX
   Public cbSize As UInt32
   Public fMask As UInt32
   Public hwnd As IntPtr
   Public lpVerb As IntPtr
   Public lpFile As IntPtr
   Public lpParameters As IntPtr
   Public lpDirectory As IntPtr

   Public nShow As Integer
   Public hInstApp As IntPtr
   ' Optional members
   Public lpIDList As IntPtr
   Public lpClass As IntPtr
   Public hkeyClass As IntPtr
   Public dwHotKey As UInt32
   Public hIcon As IntPtr
   Public hProcess As IntPtr
End Class 'SHELLEXECUTEEX

<DllImport("coredll")>  _
Private Shared Function ShellExecuteEx(ex As SHELLEXECUTEEX) As Integer

<DllImport("coredll")>  _
Private Shared Function LocalAlloc(flags As Integer, size As Integer) As
IntPtr

<DllImport("coredll")>  _
Private Shared Sub LocalFree(ptr As IntPtr)

' Code starts here
Dim docname As String = "\windows\default.htm"
Dim nSize As Integer = docname.Length * 2 + 2
Dim pData As IntPtr = LocalAlloc(&H40, nSize)
Marshal.Copy(Encoding.Unicode.GetBytes(docname),0,pData,nSize - 2)

   Dim see As New SHELLEXECUTEEX()
   see.cbSize = 60
   see.dwHotKey = 0
   see.fMask = 0
   see.hIcon = IntPtr.Zero
   see.hInstApp = IntPtr.Zero
   see.hProcess = IntPtr.Zero
   see.lpClass = IntPtr.Zero
   see.lpDirectory = IntPtr.Zero
   see.lpIDList = IntPtr.Zero
   see.lpParameters = Pathto Fileto beam
   see.lpVerb = IntPtr.Zero
   see.nShow = 0
   see.lpFile = pData

   ShellExecuteEx(see)

   LocalFree(pData)
0
 
rahjinohAuthor Commented:
The code didn't really work... not out of the box at least.
But after serveral modifications, and editing, i finally managed to get the beam.exe to send files!

Thank you so much for your time and patience :)

Really appreciate that. Thx, i upped the points for you :)
0
 
Mikal613Commented:
Ohhh  thanks alot

Woo hoo we did it!!

And I just took a guess that the Beam would have a parameter
0
 
rahjinohAuthor Commented:
that's a great guess, hehe, heck i didn't even realised there's a Beam.exe in the Windows folder, seems like i better explore the Windows folder more.

I had thought the beaming of files was done thu a API Call to explorer.exe

Oh, and there's just a little tinny question, After i create that  new process to beam files, my original app killed itself. Any idea why and how do i stop that? :)
0
 
Mikal613Commented:
well where did you put the function dont put it in your startup function

Try debugging it
0
 
rahjinohAuthor Commented:
it wasn't a startup, anyway, i managed to stop it, abeit a lame way :D
hehe
0
 
Mikal613Commented:
Can you please send me your final code

Thanks a Ton!!!!
0
 
rahjinohAuthor Commented:
Imports System.Runtime.InteropServices

Public Class Wireless
  <DllImport("CoreDll.DLL", SetLastError:=True)> _
  Private Shared Function CreateProcess(ByVal imageName As String, ByVal cmdLine As String, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, ByVal boolInheritHandles As Int32, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpszCurrentDir As IntPtr, ByVal si() As Byte, ByVal pi As ProcessInfo) As Integer
  End Function

  <DllImport("CoreDll.dll")> _
  Private Shared Function GetLastError() As Int32
  End Function

  Public Shared Function CreateProc(ByVal ExeName As String, ByVal CmdLine As String, ByVal pi As ProcessInfo) As Boolean
    If pi Is Nothing Then
      pi = New ProcessInfo
    End If
    Dim si(127) As Byte
    Return CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) <> 0
  End Function 'CreateProc
End Class 'Externals

Public Class ProcessInfo
  Public hProcess As IntPtr
  Public hThread As IntPtr
  Public ProcessId As Int32
  Public ThreadId As Int32
End Class 'ProcessInfo

Wireless.CreateProc("\Windows\Beam.exe", filename, nothing)
0
 
Mikal613Commented:
you didnt use shellexecute?
0
 
rahjinohAuthor Commented:
there's no ShellExecute in .NET CF
0
 
Mikal613Commented:
there actually is 11 posts above , it works also

Thanks for posting

;)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 21
  • 18
Tackle projects and never again get stuck behind a technical roadblock.
Join Now