我在C#中开发了一个简单的类库,并希望通过VB6中的COM Interop公开他的方法和变量
C#解决方案在组件文件中发布了COM Enabled,+ Interface + GUID标识符,以及接口和唯一功能。
当我添加发布后生成的TLB文件时,VB6 IDE很好地链接文件,我可以在设计时看到方法和属性,但是当我执行代码时,我收到错误#429“无法创建ActiveX对象”
我尝试使用命令'regasm','gacutil'注册DLL文件,但仍无法从VB6(C#类库)运行DLL文件的方法
AssemblyInfo文件:
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("ConnectToAPI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConnectToAPI")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(true)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f29d88e8-cd96-490a-8dc1-3bf68852a396")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
和ClassLibrary:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ConnectToAPI
{
[ComVisible(true)]
[Guid("41B3F5BC-A52B-4AED-90A0-F48BC8A391F1")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISharconn
{
//Authentication
string url { get; set; }
string user { get; set; }
string password { get; set; }
string token { get; set; }
dynamic stuff { get; set; }
string upload { get; set; }
string name { get; set; }
string expires_in { get; set; }
string access_token { get; set; }
string teamId { get; set; }
//video object properties
//**********************************
string Path { get; set; } //cambiar para que sea el path correcto
int Size { get; set; }
string ContentType { get; set; }
string UserId { get; set; }
int FolderId { get; set; }
int QueueId { get; set; }
string ThumbnailPath { get; set; }
string ChaptersPath { get; set; }
string Description { get; set; }
string Name { get; set; }
List<ShareUsers> ShareUser { get; set; }
List<TagsUpload> Tags { get; set; }
List<CommentUpload> Comments { get; set; }
List<DocumentUpload> Documents { get; set; }
List<CommentUsers> UsersReceiveComments { get; set; }
string StartConnection();
void AddVideoTags(int tagId, string description);
void AddShareUsers(string id, string username, bool allowDownload);
void AddVideoComments(string commentText, bool status);
void AddDocuments(string path, string ContentType, int Size);
string GetTree(string url, string access_token);
string UploadVideo(string url, string access_token);
void AddUserListComments(string sendtoId, string sendtoUsername);
void GetToken(string url, string userName, string password);
}
[ComVisible(true)]
[Guid("20EBC3AF-22C6-47CE-B70C-7EBBA12D0A29")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ConnectToAPI.Sharconn")]
public class Sharconn : ISharconn
{
//Authentication
public string url { get; set; }
public string user { get; set; }
public string password { get; set; }
public string token { get; set; }
public dynamic stuff { get; set; }
public string upload { get; set; }
public string name { get; set; }
public string expires_in { get; set; }
public string access_token { get; set; }
public string teamId { get; set; }
//video object properties
//**********************************
public string Path { get; set; } //cambiar para que sea el path correcto
public int Size { get; set; }
public string ContentType { get; set; }
public string UserId { get; set; }
public int FolderId { get; set; }
public int QueueId { get; set; }
public string ThumbnailPath { get; set; }
public string ChaptersPath { get; set; }
public string Description { get; set; }
public string Name { get; set; }
private List<ShareUsers> shareUser = new List<ShareUsers>();
public List<ShareUsers> ShareUser
{
set { shareUser = value; }
get { return shareUser; }
}
private List<TagsUpload> tags = new List<TagsUpload>();
public List<TagsUpload> Tags
{
set { tags = value; }
get { return tags; }
}
private List<CommentUpload> comments = new List<CommentUpload>();
public List<CommentUpload> Comments
{
set { comments = value; }
get { return comments; }
}
private List<DocumentUpload> documents = new List<DocumentUpload>();
public List<DocumentUpload> Documents
{
set { documents = value; }
get { return documents; }
}
private List<CommentUsers> usersReceiveComments = new List<CommentUsers>();
public List<CommentUsers> UsersReceiveComments
{
set { usersReceiveComments = value; }
get { return usersReceiveComments; }
}
public string StartConnection() {
upload = UploadVideo(url + "api/Videos/UploadVideo", access_token);
//Access API controller
var client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", access_token);
var response = client.GetAsync("api/data/authorize").Result;
if (response.IsSuccessStatusCode)
{
Trace.TraceInformation("Success");
// MessageBox.Show("Success");
}
string message = response.Content.ReadAsStringAsync().Result;
Trace.TraceInformation("URL response: " + message);
//MessageBox.Show("URL response: " + message);
return message;
}
public void GetToken(string url, string userName, string password)
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>( "grant_type", "password" ),
new KeyValuePair<string, string>( "username", userName ),
new KeyValuePair<string, string> ( "password", password )
};
var content = new FormUrlEncodedContent(pairs);
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient())
{
var response = client.PostAsync(url + "token", content).Result;
string text = response.Content.ReadAsStringAsync().Result;
stuff = JsonConvert.DeserializeObject(text);
name = stuff.token_type;
expires_in = stuff.expires_in;
access_token = stuff.access_token;
}
}
public void AddVideoTags(int tagId, string description)
{
TagsUpload tag = new TagsUpload();
tag.TagId = tagId;
tag.TagDescription = description;
tags.Add(tag);
}
public void AddShareUsers(string id, string username, bool allowDownload)
{
ShareUsers share = new ShareUsers(); //lista de usuarios a los que se le comparte el video
share.Id = id; //id del usuario al que se le comparte
share.Username = username;
share.AllowDownload = allowDownload;
share.NotificationPublic = false; //siempre en false
share.NotificationComment = false; //siempre en false
shareUser.Add(share);
}
public void AddUserListComments(string sendtoId, string sendtoUsername)
{
CommentUsers users = new CommentUsers(); //usuario al que se le envía el comentario
users.Id = sendtoId;
users.Username = sendtoUsername;
usersReceiveComments.Add(users);
}
public void AddVideoComments(string commentText, bool status) {
CommentUpload c = new CommentUpload();
c.CommentText = commentText;
c.Public = status; //false;
c.Receivers = usersReceiveComments;
comments.Add(c);
}
public void AddDocuments(string path, string ContentType, int Size)
{
DocumentUpload doc = new DocumentUpload();
doc.Path = path;
doc.ContentType = ContentType;
doc.Size = Size;
documents.Add(doc);
}
public string GetTree(string url, string access_token)
{
//tree = GetTree(url + "api/Folders/GetTree", access_token);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", access_token);
Team team = new Team();
team.Id = teamId;
var teamObj = JsonConvert.SerializeObject(team);
var response = client.PostAsync(url, new StringContent(teamObj, Encoding.UTF8, "application/json")).Result;
//recibe el token desde el servidor
string text = response.Content.ReadAsStringAsync().Result;
return text;
//tree = text;
}
}
public string UploadVideo(string url, string access_token)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", access_token);
//***************************************
//SET VIDEO VALUES
//***************************************
Video v = new Video();
v.Path = Path; //cambiar para que sea el path correcto
v.Size = Size; //tamaño en bytes del video
v.ContentType = ContentType; // "video/mp4";
v.UserId = UserId; //"b93232eb-6deb-4eae-b252-a633e1264f0f"; //usuario que sube el video
v.FolderId = FolderId; //41; //Id de la carpeta en la que se guarda el video. Si es 0 entonces solo va al directorio raíz
v.QueueId = QueueId;//0; //id del archivo en la cola de subida. Es 0 si nunca se pausó la descarga.
v.ThumbnailPath = ThumbnailPath; // "C://VideoPrueba/presentacion icse/atencion1.jpg"; //path de la imagen de presentación del video. Thumbnail
v.ChaptersPath = ChaptersPath; // "C://VideoPrueba/chapters.vtt"; //path de los episodios o capitulos del video en formato vtt
v.Description = Description; // "Descripción de video";
v.Name = Name; //nombre del video, si viene "" entonces se toma el nombre del archivo.
v.Share = shareUser;
v.Tags = tags;
v.Comments = comments;
v.Docs = documents;
//private List<ShareUsers> shareUser = new List<ShareUsers>();
//private List<TagsUpload> _tags = new List<TagsUpload>();
//private List<CommentUpload> _comments = new List<CommentUpload>();
//private List<DocumentUpload> _documents = new List<DocumentUpload>(); //lista de docs a subir. (jpeg, pdf, etc)
v.ErrorCode = 0; //codigo de error (su función se da solo en la respuesta). En la petición siempre es 0
//return v;
var videoObj = JsonConvert.SerializeObject(v);
var response = client.PostAsync(url, new StringContent(videoObj, Encoding.UTF8, "application/json")).Result;
//recibe la respuesta que es un JSON con el objeto Video
//Verificar el error_code. Si es 0 todo fue correcto, sino hubo un error
string text = response.Content.ReadAsStringAsync().Result;
return text;
}
}
}
public class Team
{
public string Id { get; set; }
}
public class ShareUsers
{
public string Id { get; set; }
public string Username { get; set; }
public bool AllowDownload { get; set; }
public bool NotificationPublic { get; set; }
public bool NotificationComment { get; set; }
}
public class TagsUpload
{
public int TagId { get; set; }
public string TagDescription { get; set; }
}
public class CommentUpload
{
public string CommentText { get; set; }
public bool Public { get; set; }
public List<CommentUsers> Receivers { get; set; }
}
public class CommentUsers
{
public string Id { get; set; }
public string Username { get; set; }
}
public class DocumentUpload
{
public string Path { get; set; }
public int Size { get; set; }
public string ContentType { get; set; }
}
public class Video
{
public string Path { get; set; }
public string ContentType { get; set; }
public int Size { get; set; }
public string UserId { get; set; }
public int VideoId { get; set; }
public int ErrorCode { get; set; }
public List<ShareUsers> Share { get; set; }
public int FolderId { get; set; }
public List<TagsUpload> Tags { get; set; }
public List<CommentUpload> Comments { get; set; }
public List<DocumentUpload> Docs { get; set; }
public int QueueId { get; set; }
public string ThumbnailPath { get; set; }
public string ChaptersPath { get; set; }
public string Description { get; set; }
public string Name { get; set; }
}
}
VB6代码,它包含对创建的类库的引用,我只能在设计时看到DLL的方法和属性:
Private Sub Command1_Click()
Dim connectToAp As New connectToApi.Sharconn
connectToAp.url = "http://test.azurewebsites.net"
connectToAp.user = "asas"
connectToAp.password = "freng$"
connectToAp.StartConnection
'OBTENGO EL TOKEN Y SUS PROPIEDADES
Call connectToAp.GetToken(connectToAp.url, connectToAp.user, connectToAp.password)
Dim tokenName As String
tokenName = connectToAp.Name
Dim tokenExpires As String
tokenExpires = connectToAp.expires_in
Dim accessToken As String
accessToken = connectToAp.access_token
'VALORES DEL VIDEO
Dim Path As String
Path = connectToAp.Path
Dim Size As Integer
Size = connectToAp.Size
Dim ContentType As String
ContentType = connectToAp.ContentType
Dim UserId As String
UserId = connectToAp.UserId
Dim FolderId As Integer
FolderId = connectToAp.FolderId
Dim QueueId As Integer
QueueId = connectToAp.QueueId
Dim ThumbnailPath As String
ThumbnailPath = connectToAp.ThumbnailPath
Dim ChaptersPath As String
ChaptersPath = connectToAp.ChaptersPath
'ANADE LOS TAGS AL VIDEO, ID MAS NOMBRE. SI EL ID ES 0 QUIERE DECIR QUE EL TAG ES NUEVO
' Y SE GUARDA EN LA BD DE TAGS EXISTENTES
Dim tagId As Long
Dim tagDescription As String
Call connectToAp.AddVideoTags(tagId, tagDescription)
'ANADE LOS USUARIOS A LOS QUE SE LE VA A COMPARTIR EL VIDEO
Dim idUser As String
Dim username As String
Dim allowDownload As Boolean
Call connectToAp.AddShareUsers(idUser, username, allowDownload)
Dim commentText As String
Dim isPublic As Boolean
Call connectToAp.AddVideoComments(commentText, isPublic)
Dim sendtoId As String
Dim sendtoUsername As String
Call connectToAp.AddUserListComments(sendtoId, sendtoUsername)
End Sub