EXCEL.EXE process closes immeditately when using COM Interop - interop

I'm using COM Interop to access Excel from a Centura Team Developer program and the EXCEL.EXE process exits immediately after calling Create in my Application. All later calls throw exception because Excel is not open anymore.
What could be the reason for this behaviour?

Because you did not free all Variable ref to COM Object. You need to set the Variable ref to COM Object as null.
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
OleConn.Close();
OleConn = null;
like OleConn, you need to free all Variable ref to COM Object.

Related

How to use SuppressGetDecimalInvalidCastException in vb.net

With some vb.net code I try to retrieve data from an Oracle database (simplified example):
strQuery = "Select 2.3, 2.3/1, 2.3/3.1 From Owner.TableName Where ROWNUM < 10"
Dim da As OracleDataAdapter
da = New OracleDataAdapter(strQuery, ConnectionString)
da.Fill(GetData)
This results in an "Specified cast is invalid" error.
The 2.3/3.1 is the problem.
I learned from "Specified cast is not valid" when populating DataTable from OracleDataAdapter.Fill() that Oracle works with a higher precision than dot net can handle and that I should use SuppressGetDecimalInvalidCastException in the OracleDataAdapter. But I don't know how to code it in VB.net. Can anyone help me?
Automatic translation from the C# code did not work.
The C# code itself did not work for me (probably due to the fact that I don't know how to handle the async stuff) and if I simplify it to
string queryString = "Select 2.3, 3.1 From owner.table";
string connectionString = "Data Source=Data.plant.be/database;User ID=****;Password=****";
var table = new DataTable();
var connection = new OracleConnection(connectionString);
var command = new OracleCommand(queryString, connection);
var adapter = new OracleDataAdapter(command) {
SuppressGetDecimalInvalidCastException = true
};
adapter.Fill(table);
I get error CS0117: OracleDataAdaptor does not contain a definition for SuppressGetDecimalInvalidCastException.
Extra info:
As proposed by #Andrew-Morton - thank you Andrew - I wrote:
Dim table = New DataTable()
Dim connection = New OracleConnection(ConnectionString)
Dim cmd = New OracleCommand(strQuery, connection)
Dim adapter = New OracleDataAdapter(cmd) With {.SuppressGetDecimalInvalidCastException = True}
adapter.Fill(GetData)
But I get BC30456: SuppressGetDecimalInvalidCastException is not a member of 'OracleDataAdapter'.
Remark: I have version 19.6 of Oracle.ManagedDataAccess.
I could not install package 'Oracle.ManagedDataAccess 21.9.0'. I Get: You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

XMS IBytesMessage causing problems with split ZIP file

Since upgrading MQ to "IBM MQ Explorer V9.1", the XMS libraries that always worked in previous versions have started behaving differently.
Essentially, the code still recognises the messages as IBytesMessage type, and successfully writes them to file via a Byte array, but the file itself, which is a split zip file, fails to reconstitute itself.
Here's the lions share of that code:
Dim FactoryFactory As XMSFactoryFactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ)
'Create a ConnectionFactory Object
cfConnectionFactory = FactoryFactory.CreateConnectionFactory()
'this variable will contain the full path of any file downloaded from MQ
Dim strMQMessageOutputFileDestinationFilePath As String = ""
'This variable will be used to evaluate whether the MQ Message Output file exists
Dim fiMQMessageOutputFile As FileInfo = Nothing
'Set various Connection Factory properties
cfConnectionFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, Me.HostName)
cfConnectionFactory.SetIntProperty(XMSC.WMQ_PORT, 1414)
cfConnectionFactory.SetStringProperty(XMSC.WMQ_CHANNEL, "SYSTEM.DEF.SVRCONN")
cfConnectionFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, 1)
cfConnectionFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, Me.QueueManager)
cfConnectionFactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, 0)
'Create a new Iconnection object via the Connection Factory
connection = cfConnectionFactory.CreateConnection()
'Create a sesion via the Connection Object, using ClientAcknowledge mode
'ClientAcknowledge is being used because it allows us to control precisely
'when a message should be removed from the queue
session = connection.CreateSession(False, AcknowledgeMode.ClientAcknowledge)
'Create a destination using the Session Object
destination = session.CreateQueue(Me.mstrDestinationURI)
destination.SetIntProperty(XMSC.DELIVERY_MODE, 1)
'Create a consumer using the Session & Destination Objects
Consumer = session.CreateConsumer(destination)
connection.Start()
'IMessage is the base class that is returned from the Consumer's Receive method
Dim recvMsg As IMessage = Nothing
' Retrieve message from Queue
recvMsg = Consumer.ReceiveNoWait
strFileNameFromMsg = If(Not recvMsg.PropertyExists("fileName"), "",
recvMsg.GetStringProperty("fileName"))
If TypeOf (recvMsg) Is IBytesMessage Then
'Binary Message
Dim msg As IBytesMessage = CType(recvMsg, IBytesMessage)
Dim buffer(msg.BodyLength) As Byte
msg.ReadBytes(buffer)
Dim content As String = Text.Encoding.UTF8.GetString(buffer)
'The PrepareDestinationFile Function will generate a unique file name for the new file
'and ensure that the file does not already exist on the drive
strMQMessageOutputFileDestinationFilePath = PrepareDestinationFile(strFileNameFromMsg)
'A FileStream object is needed to write a binary array to a file
Dim fsZipFile As FileStream = New FileStream(strMQMessageOutputFileDestinationFilePath, FileMode.Create)
'Write the contents of the Byte Array to the File via the FileStream object
fsZipFile.Write(buffer, 0, buffer.Length)
fsZipFile.Close()
End If
So, the code doesn't throw any kind of exception - the code still recognises the messages as IBytesMessage, but the files won't unzip correctly.
Oddly, If we use rfhutilc.exe, we can manually pull files provided we set the Write options as No Headers and not Include MQMD - but the code above always worked in the previous version of MQ / XMS
Any assistance you can provide would be very much appreciated.

Insert in Access Database via VB / Visual Studio (2017)

I´m trying to develop a system that reproduces a quiz game. Basically it´s developed with vb (vs 2017) and microsoft access database.
In certain point after the player choose the wrong answer, the system compiles the results with name, amount of righ questions and maximum score.
Aftewards it should record in the database the results., but, it doensnt make it.
My code follow bellow:
Sub RegistraJogo()
Try
Dim Conn As New OleDbConnection
Conn.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=DbDesafioQuiz.accdb"
Conn.Open()
Dim cmd = Conn.CreateCommand
cmd.CommandText = "INSERT INTO TbRecordes (Nome, Data, Pontos, Acertadas) VALUES (#Jogador, #Datas, #Pts, #Corretas)"
cmd.Parameters.AddWithValue("#Jogador", LbJogador.Text) 'nome do jogador
cmd.Parameters.AddWithValue("#Datas", CStr(Now())) 'data do jogo
cmd.Parameters.AddWithValue("#Pts", CStr(PontosTotais * QtdRespondidas)) 'variáveis com valores de pontos e qtd de questões resp
cmd.Parameters.AddWithValue("#Corretas", QtdRespondidas) 'qtd de questões respondidas
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
receivedData = 0
End Sub
Someone could help me, indicanting what i´m doing wrong? Thanks in advance.
I´ve changed the connection string, putting the complete address of database, like:
"c:\temp\dbdesafioquiz.accdb"

Update function not working in vb.net

Currently I'm develop a system using VB.NET. I have the following query for UPDATE. This query is work when I run in SQL Developer
UPDATE CCS2_TBL_INSPECTION_STANDARD SET CCSEQREVITEM = :CCSEQREVITEM,
CCSREVEFFECTIVEDATE = TO_DATE(:CCSREVEFFECTIVEDATE,'DD/MM/YYYY') WHERE
CCSEQID = :CCSEQID
But when I try applied this query in VB.net, its not work. Actually the flow for this update function is work but when I update the data, it is not working. For example, I want update name from 'Ali' to 'Abu', when I click the update button, there popup windows says that "Update success" but the name is not change to 'Abu', it still 'Ali'. There no error when I execute. Anyone know? Below VB.net code:
Protected Sub editInspectionRev(eqid As String)
Dim xSQL As New System.Text.StringBuilder
xSQL.AppendLine("UPDATE CCS2_TBL_INSPECTION_STANDARD")
xSQL.AppendLine("SET")
xSQL.AppendLine("CCSEQREVITEM = :CCSEQREVITEM, CCSREVEFFECTIVEDATE = TO_DATE(:CCSREVEFFECTIVEDATE,'DD/MM/YYYY')")
xSQL.AppendLine("WHERE CCSEQID = :CCSEQID")
Using cn As New OracleConnection(ConString)
cn.Open()
Dim cmd As New OracleCommand(xSQL.ToString, cn)
cmd.Connection = cn
cmd.Parameters.Add(":CCSEQREVITEM", txtRevContent.Text)
cmd.Parameters.Add(":CCSREVEFFECTIVEDATE", txtRevEffDate.Text)
cmd.Parameters.Add(":CCSEQID", eqid)
cmd.ExecuteNonQuery()
cn.Close()
End Using
success3.Visible = True
DisplayRevisionDetails()
End Sub
The problem is that you have executed the transaction but failed to COMMIT it. There is an example of the correct method here, which I will reproduce in part below for posterity
Using connection As New OracleConnection(connectionString)
connection.Open()
Dim command As OracleCommand = connection.CreateCommand()
Dim transaction As OracleTransaction
' Start a local transaction
transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)
' Assign transaction object for a pending local transaction
command.Transaction = transaction
...
command.ExecuteNonQuery()
transaction.Commit()
Observe that we have begun the transaction, and then committed it after executing.

Is it possible to duplicate the following credential process in VB.NET?

Solution (kinda):
Turns out this impersonation with .NET's security only allows application-level access. Since the COM object is at the system level, the impersonated user still cannot instantiate it. I figured this out by right-clicking the executable and selecting "Run As...", the program functioned fine. I found out that launches the program with system access (assuming the user you are running it with has those credentials). Now I am in the process of creating an external program that will launch this application using this method.
Thanks for the tips :D
I have a windows XP installation on a virtual machine. It is part of my domain, but the logged in user is a local user only. Obviously, if I try to access a network share it will prompt for a user/password:
The program I am testing out on the virtual machine uses a COM object to interface with data from another program. If I do not impersonate, I get errors because I do not have the proper credentials.
I did some research into the matter and found a number of websites that had a decent amount of VB.NET information. The problem I am having with the code I wrote is I can access the network resources, but I cannot instantiate the COM object.
If I fill and submit the credential prompt (above) before attempting to instantiate it, it works fine. That leads me to believe there must be something that the WinXP credential prompt is doing that I am not. Below is the code I am using for Impersonation:
Public Sub BeginImpersonation()
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2
Dim win32ErrorNumber As Integer
_tokenHandle = IntPtr.Zero
_dupeTokenHandle = IntPtr.Zero
If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname)
End If
If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then
win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
CloseHandle(_tokenHandle)
Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname)
End If
Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
_impersonatedUser = newId.Impersonate()
_impersonating = True
End Sub
I have also tried sending different flags to the impersonator method, but nothing seems to be working. Here are the different flags I found:
Enum LOGON32_LOGON
INTERACTIVE = 2
NETWORK = 3
BATCH = 4
SERVICE = 5
UNLOCK = 7
NETWORK_CLEARTEXT = 8
NEW_CREDENTIALS = 9
End Enum
Enum LOGON32_PROVIDER
[DEFAULT] = 0
WINNT35 = 1
WINNT40 = 2
WINNT50 = 3
End Enum
Enum SECURITY_LEVEL
Anonymous = 0
Identification = 1
Impersonation = 2
Delegation = 3
End Enum
I have run into this before, and used two different soloution - the easiest was using a third party app: TqcRunas: http://www.quimeras.com/Products/products.asp which allows you to package the required creentials in an encrypted file. However is a pain if the password is forced to expire.
The other solution that I have used is to call a new process with alternative credentials:
Dim myProcessStartInfo As ProcessStartInfo = New ProcessStartInfo
With myProcessStartInfo
.FileName = "file path and name"
.Domain = "domainname"
.UserName = "username"
'password needs to be a SerureString
Using NewPassword As New Security.SecureString
With NewPassword
For Each c As Char In "password".ToCharArray
.AppendChar(c)
Next c
.MakeReadOnly()
End With
.Password = NewPassword.Copy
End Using
'UseShellExecute must be false for impersonated process
.UseShellExecute = False
End With
Using Process As New System.Diagnostics.Process
With Process
.StartInfo = myProcessStartInfo
.Start()
End With
End Using
With your definitions, I use
LogonUser(_username, _domainname, _password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, _tokenHandle)
in my code that is authenticating across the network. I am impersonating a local user on the remote box, as you are. It was so long ago that I don't remember the rationale for using these values, however.
I do a similar thing to map network drives for copying files between machines. I didn't write the code but it's pretty much the same as yours, except for two things:
After the Impersonate method returns I close both tokens using the CloseHandle routine, before I exit my impersonator method.
At the top of the impersonator the first thing that happens is a call to RevertToSelf, presumably to cancel any previous impersonation.
I don't know if they would make a difference but it's worth a try. Here are the relevant declarations:
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long

Resources