I'm trying to use a object made id VB6 into my c# project, but I don't have the VB6 source code. A did this steps:
1) register the mail.dll (thats a VB6 dll);
2) add the reference that appears on COM;
3) import to my code.
I create an object. But when I tried to call the method SendMailSMTP from this dll I got this error:
ActiveX component can't create object(429)
This error is common when the DLL is not registered. But a registered with success.
Is there any other way to create this VB6 dll with correct interoperability ?
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTPMail;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string msgRetorno = "";
clsSMTPmail smtpMail = new clsSMTPmail();
string from = "test#test.com.br";
string to = "test#test.com.br"; ;
string cc = "";
string subject = "teste subject";
string corpo = "Mensagem de teste";
string profileSMTP = "";
string passwordSMTP = "";
try
{
msgRetorno = smtpMail.SendMailSMTP(from, to, cc, subject, corpo, profileSMTP, passwordSMTP, null);
}
catch (Exception e)
{
msgRetorno = e.InnerException.Message;
}
}
}
}
Related
I'm using String interpolation in the code behind, and now I need to take part of it to a class.
when I do it, I get error "CS1056: Unexpected character '$'"
even a very simple code gives the error right on running (not on build):
string MailSubject = $"this is your score: {userScore}";
this part of code is part of the FaceClass.CS file
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
namespace ns.App_Code
{
public class FakeClass
{
public static void Check_Next_In_Line(int score)
{
int temp = Fake2Class.GetData();
if (temp == 0)
{
string MailSubject = "";
string MailBody = "";
MailBody = $"Your score: {score}";
/*
mail send function
*/
}
}
}
}
I'm using .NET Framework 4.8
String Interpolation works for me in a aspx code behind but not in a method within a class. If I want to refactor a part of code becuase it is needed more than once - it won't work
Hi an alternative solution to what you are looking for may be would be to use string format. Something like below
int userscore;
string MailSubject = string.Format("this is your score: {0}", userscore);
This question already has an answer here:
calling .NET COM object from VBScript
(1 answer)
Closed last year.
I have created a COM dll written in C#. It is registered successfully and I am trying to access it in VB script. I am able to create the object of the COM and am able to execute first method call.
When I try to execute the second method call (msg = app.getSubDomainConn( domain)), with the object returned by first method call, I get following error. I have posted both VBscript and C# code.
Could someone please help?
Error: Invalid procedure call or argument: 'getValidConnectionMsg'
Code: 800A0005
Source: Microsoft VBScript runtime error
dim app
Set app = CreateObject("myCOM.myObject.")
app.uri=<URI>
app.username=<User>
app.password=<pwd>
Dim domain
Set domain = app.getDomain()
domain.ok = true;
wscript.echo domain.name
dim msg
msg = app.getSubDomainConn(domain)
Here is C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myCOM
{
public class myObject
{
public String uri = "";
public String username = "";
public String password = "";
public IDomain getDomain()
{
String msg = "";
IDomain domain = null;
try
{
// Get domain Connection
IConnection conn = Factory.Connection.GetConnection(uri);
UserCredentials creds = new UsernameCredentials(username, password);
// Get domain.
domain = Factory.Domain.Get(conn);
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
}
return domain;
}
public String getValidConnectionMsg(IDomain domain)
{
String isDomainAvailable = "Not Available";
if (domain != null)
{
isDomainAvailable = domain.Name;
}
return isDomainAvailable;
}
}
}
The problem was that VB script variant datatype only considers object. I had to make a change in C# code. Instead of accepting it as a IDomain object, It should have been an Object type argument.
Instead of below
public String getValidConnectionMsg(IDomain domain)
Modified code is
public String getValidConnectionMsg(Object d)
{
IDomain domain = (IDomain)d;
.
.
.
I tring to test a new dll that I've build for c#
private void button1_Click(object sender, EventArgs e)
{
String [] first = UserQuery.Get_All_Users();
//MessageBox.Show(first);
}
but I get the following error at String [] first = UserQuery.Get_All_Users();
An unhandled exception of type 'System.NullReferenceException' occurred in User_Query.dll
Additional information: Object reference not set to an instance of an object.
I been tring to figure this one out for hours but can't find any null varibles
I post my dll in case the dll is wrong
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace User_Query
{
public class UserQuery
{
public static string[] Get_All_Users()
{
string[] names = new string[10];
var path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (var computerEntry = new DirectoryEntry(path))
{
var userNames = from DirectoryEntry childEntry in computerEntry.Children
where childEntry.SchemaClassName == "User"
select childEntry.Name;
byte i = 0;
foreach (var name in userNames)
{
Console.WriteLine(name);
names[i] = name;
i++;
}
return names;
}
}
}
}
There is a problem with your. path variable... since there should be \\ instead of //
The problem here turned out not to be the code but be VS2010 not loading the dll. This happen because I decided to change the program from using the dll from the debug to the release version but I did not clean the project after doing it and therefore the program was not correctly loading the dll. All that need to be done was clean the project
I'm new to code and most things work, but I can't get this code to run. Can someone help?
I tried using System.Forms but it showed as missing a namespace. When I used using System.Windows.Forms that message went away. It does not let me use both.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(#"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(#"file.csv", Encoding.GetEncoding(1250));
string strline = "";
String[] _values = null;
int x = 0;
while(!sr.EndOfStream)
{
strline = sr.ReadLine();
_values = strline.Split(',');
if (_values.Length >= 6 && _values[0].Trim().Length > 0)
{
MessageBox.show(_values[1]);
}
}
sr.Close();
}
}
}
There is no such namespace System.Forms, the class you were trying to use (MessageBox) is in System.Windows.Forms. By correcting your using statement, the error went away.
Remember, you must have a reference to System.Windows.Forms.dll in your console app to use this class.
You need to reference System.Windows.Forms.dll in your project. Here is a detailed instruction how to do that.
There is no such namespace as System.Forms there is only a namespace called System.Windows.Forms, wich has the MessageBox class you are talking about. To be able to use it, you need to add a reference to the System.Windows.Forms.dll to to your project (find it in the .NET Tab in the "Add Reference ..." dialog) and it will work. Also note that MessageBox.Show() requires a capital 'S'. Please see below an optimized and fully working version of your code.
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
using (StreamReader sr = new StreamReader(#"file.csv"))
{
while (sr.Peek() >= 0)
{
string strline = sr.ReadLine();
string[] values = strline.Split(',');
if (values.Length >= 6 && values[0].Trim().Length > 0)
{
MessageBox.Show(values[1]);
}
}
}
}
}
}
You try use it in Console application first you should add System.Windows.Forms dll in your references (from .Net reference tab) then use it by adding it's namespace.
I'm a bit confused here. there is no namespace called System.Forms. It's always System.Windows.Forms. And the MessageBox class is defined in System.Windows.Forms
You need to manually ADD a reference to your project for System.Windows.Forms as you are on a console application and not a Windows Application. Just add the reference.
How can I add a C# object to JavaScript code?
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;
using Microsoft.Vsa;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.Run();
}
public object Run()
{
VsaEngine Engine = VsaEngine.CreateEngine();
object Result = null;
try
{
//Engine.PushScriptObject(new ScriptObject());
// C# object replace with ?
Result = Eval.JScriptEvaluate("var a = 1; var b = c# object", Engine);
}
catch (Exception ex)
{
return ex.Message;
}
return Result;
}
}
}
Just compile your C# object into an assembly and load that assembly in your jscript code. all the languages that .NET supports can load each other's assemblies.
You can take a look at Script#
As they say,
"Script# brings the power and productivity of C# and .NET tools to Ajax development by compiling C# source code into regular JavaScript."